Merge branch 'master' into copy_ast

This commit is contained in:
2017-05-10 23:13:40 -04:00
11 changed files with 159 additions and 17 deletions

1
.gitignore vendored
View File

@@ -13,6 +13,7 @@ tests*/*cpp
tests*/*run
tests*/*.o
tests_ispcpp/*.h
tests_ispcpp/*.out
tests_ispcpp/*pre*
logs/
notify_log.log

View File

@@ -1927,6 +1927,11 @@ FunctionEmitContext::BinaryOperator(llvm::Instruction::BinaryOps inst,
return NULL;
}
if (v0->getType() != v1->getType()) {
v0->dump();
printf("\n\n");
v1->dump();
}
AssertPos(currentPos, v0->getType() == v1->getType());
llvm::Type *type = v0->getType();
int arraySize = lArrayVectorWidth(type);

View File

@@ -606,6 +606,7 @@ lDoTypeConv(const Type *fromType, const Type *toType, Expr **expr,
"\"%s\" for %s", fromType->GetString().c_str(),
toPolyType->GetString().c_str(), errorMsgBase);
}
return false;
}
}
@@ -7209,8 +7210,11 @@ TypeCastExpr::GetValue(FunctionEmitContext *ctx) const {
return NULL;
return ctx->IntToPtrInst(exprVal, llvmToType, "int_to_ptr");
}
else {
} else if (CastType<PolyType>(toType)) {
Error(pos, "Unexpected polymorphic type cast to \"%s\"",
toType->GetString().c_str());
return NULL;
} else {
const AtomicType *toAtomic = CastType<AtomicType>(toType);
// typechecking should ensure this is the case
if (!toAtomic) {

View File

@@ -651,8 +651,10 @@ Function::ExpandPolyArguments(SymbolTable *symbolTable) const {
const FunctionType *func = CastType<FunctionType>(sym->type);
printf("%s before replacing anything:\n", sym->name.c_str());
code->Print(0);
if (g->debugPrint) {
printf("%s before replacing anything:\n", sym->name.c_str());
code->Print(0);
}
for (size_t i=0; i<versions.size(); i++) {
const FunctionType *ft = CastType<FunctionType>(versions[i]->type);
@@ -665,13 +667,15 @@ Function::ExpandPolyArguments(SymbolTable *symbolTable) const {
ncode = (Stmt*)TranslatePoly(ncode, from,
ft->GetParameterType(j)->GetBaseType());
printf("%s after replacing %s with %s:\n\n",
sym->name.c_str(), from->GetString().c_str(),
ft->GetParameterType(j)->GetBaseType()->GetString().c_str());
if (g->debugPrint) {
printf("%s after replacing %s with %s:\n\n",
sym->name.c_str(), from->GetString().c_str(),
ft->GetParameterType(j)->GetBaseType()->GetString().c_str());
ncode->Print(0);
ncode->Print(0);
printf("------------------------------------------\n\n");
printf("------------------------------------------\n\n");
}
}
}

View File

@@ -1032,8 +1032,7 @@ Module::AddFunctionDeclaration(const std::string &name,
}
std::vector<const FunctionType *> nextExpanded;
std::set<const Type*>::iterator iter;
for (iter = toExpand.begin(); iter != toExpand.end(); iter++) {
for (auto iter = toExpand.begin(); iter != toExpand.end(); iter++) {
for (size_t j=0; j<expanded.size(); j++) {
const FunctionType *eft = expanded[j];
@@ -1060,7 +1059,14 @@ Module::AddFunctionDeclaration(const std::string &name,
nargsp.push_back(eft->GetParameterSourcePos(k));
}
nextExpanded.push_back(new FunctionType(eft->GetReturnType(),
const Type *ret = eft->GetReturnType();
if (Type::EqualForReplacement(ret, pt)) {
printf("Replaced return type %s\n",
ret->GetString().c_str());
ret = PolyType::ReplaceType(ret, *te);
}
nextExpanded.push_back(new FunctionType(ret,
nargs,
nargsn,
nargsd,
@@ -1078,6 +1084,11 @@ Module::AddFunctionDeclaration(const std::string &name,
if (expanded.size() > 1) {
for (size_t i=0; i<expanded.size(); i++) {
if (expanded[i]->GetReturnType()->IsPolymorphicType()) {
Error(pos, "Unexpected polymorphic return type \"%s\"",
expanded[i]->GetReturnType()->GetString().c_str());
return;
}
std::string nname = name;
if (functionType->isExported || functionType->isExternC) {
for (int j=0; j<expanded[i]->GetNumParameters(); j++) {
@@ -1977,6 +1988,27 @@ lPrintFunctionDeclarations(FILE *file, const std::vector<Symbol *> &funcs,
// fprintf(file, "#ifdef __cplusplus\n} /* end extern C */\n#endif // __cplusplus\n");
}
static void
lPrintPolyFunctionWrappers(FILE *file, const std::vector<std::string> &funcs) {
fprintf(file, "#if defined(__cplusplus)\n");
for (size_t i=0; i<funcs.size(); i++) {
std::vector<Symbol *> poly = m->symbolTable->LookupPolyFunction(funcs[i].c_str());
for (size_t j=0; j<poly.size(); j++) {
const FunctionType *ftype = CastType<FunctionType>(poly[j]->type);
Assert(ftype);
std::string decl = ftype->GetCDeclaration(funcs[i]);
fprintf(file, " %s {\n", decl.c_str());
std::string call = ftype->GetCCall(poly[j]->name);
fprintf(file, " return %s;\n }\n", call.c_str());
}
}
fprintf(file, "#endif // __cplusplus\n");
}
@@ -2353,8 +2385,10 @@ Module::writeHeader(const char *fn) {
// Collect single linear arrays of the exported and extern "C"
// functions
std::vector<Symbol *> exportedFuncs, externCFuncs;
std::vector<std::string> polyFuncs;
m->symbolTable->GetMatchingFunctions(lIsExported, &exportedFuncs);
m->symbolTable->GetMatchingFunctions(lIsExternC, &externCFuncs);
m->symbolTable->GetPolyFunctions(&polyFuncs);
// Get all of the struct, vector, and enumerant types used as function
// parameters. These vectors may have repeats.
@@ -2391,6 +2425,16 @@ Module::writeHeader(const char *fn) {
fprintf(f, "///////////////////////////////////////////////////////////////////////////\n");
lPrintFunctionDeclarations(f, exportedFuncs);
}
// emit wrappers for polymorphic functions
if (polyFuncs.size() > 0) {
fprintf(f, "\n");
fprintf(f, "///////////////////////////////////////////////////////////////////////////\n");
fprintf(f, "// Polymorphic function wrappers\n");
fprintf(f, "///////////////////////////////////////////////////////////////////////////\n");
lPrintPolyFunctionWrappers(f, polyFuncs);
}
#if 0
if (externCFuncs.size() > 0) {
fprintf(f, "\n");

15
sym.cpp
View File

@@ -147,7 +147,7 @@ bool
SymbolTable::AddFunction(Symbol *symbol) {
const FunctionType *ft = CastType<FunctionType>(symbol->type);
Assert(ft != NULL);
if (LookupFunction(symbol->name.c_str(), ft) != NULL)
if (LookupFunction(symbol->name.c_str(), ft, true) != NULL)
// A function of the same name and type has already been added to
// the symbol table
return false;
@@ -183,7 +183,8 @@ SymbolTable::LookupFunction(const char *name, std::vector<Symbol *> *matches) {
Symbol *
SymbolTable::LookupFunction(const char *name, const FunctionType *type) {
SymbolTable::LookupFunction(const char *name, const FunctionType *type,
bool ignorePoly) {
FunctionMapType::iterator iter = functions.find(name);
if (iter != functions.end()) {
std::vector<Symbol *> funcs = iter->second;
@@ -193,7 +194,7 @@ SymbolTable::LookupFunction(const char *name, const FunctionType *type) {
}
}
// Try looking for a polymorphic function
if (polyFunctions[name].size() > 0) {
if (!ignorePoly && polyFunctions[name].size() > 0) {
std::string n = name;
return new Symbol(name, polyFunctions[name][0]->pos, type);
}
@@ -206,6 +207,14 @@ SymbolTable::LookupPolyFunction(const char *name) {
return polyFunctions[name];
}
void
SymbolTable::GetPolyFunctions(std::vector<std::string> *funcs) {
FunctionMapType::iterator it = polyFunctions.begin();
for (; it != polyFunctions.end(); it++) {
funcs->push_back(it->first);
}
}
bool
SymbolTable::AddType(const char *name, const Type *type, SourcePos pos) {

5
sym.h
View File

@@ -181,10 +181,13 @@ public:
in the symbol table.
@return pointer to matching Symbol; NULL if none is found. */
Symbol *LookupFunction(const char *name, const FunctionType *type);
Symbol *LookupFunction(const char *name, const FunctionType *type,
bool ignorePoly = false);
std::vector<Symbol *>& LookupPolyFunction(const char *name);
void GetPolyFunctions(std::vector<std::string> *funcs);
/** Returns all of the functions in the symbol table that match the given
predicate.

13
tests_ispcpp/Makefile Normal file
View File

@@ -0,0 +1,13 @@
CXX=g++
CXXFLAGS=-std=c++11
ISPC=../ispc
ISPCFLAGS=--target=sse4-x2 -O2 --arch=x86-64
%.out : %.cpp %.o
$(CXX) $(CXXFLAGS) -o $@ $^
$ : $.o
%.o : %.ispc
$(ISPC) $(ISPCFLAGS) -h $*.h -o $*.o $<

20
tests_ispcpp/simple.cpp Normal file
View File

@@ -0,0 +1,20 @@
#include <stdlib.h>
#include <stdio.h>
#include "simple.h"
int main() {
double A[256];
for (int i=0; i<256; i++) {
A[i] = i / 11.;
}
ispc::foo(256, (double*)&A);
for (int i=0; i<256; i++) {
printf("%f\n", A[i]);
}
return 0;
}

View File

@@ -249,6 +249,15 @@ Type::IsVoidType() const {
bool
Type::IsPolymorphicType() const {
const FunctionType *ft = CastType<FunctionType>(this);
if (ft) {
for (int i=0; i<ft->GetNumParameters(); i++) {
if (ft->GetParameterType(i)->IsPolymorphicType())
return true;
}
return false;
}
return (CastType<PolyType>(GetBaseType()) != NULL);
}
@@ -3585,6 +3594,34 @@ FunctionType::GetCDeclaration(const std::string &fname) const {
return ret;
}
std::string
FunctionType::GetCCall(const std::string &fname) const {
std::string ret;
ret += fname;
ret += "(";
for (unsigned int i = 0; i < paramTypes.size(); ++i) {
const Type *type = paramTypes[i];
// Convert pointers to arrays to unsized arrays, which are more clear
// to print out for multidimensional arrays (i.e. "float foo[][4] "
// versus "float (foo *)[4]").
const PointerType *pt = CastType<PointerType>(type);
if (pt != NULL &&
CastType<ArrayType>(pt->GetBaseType()) != NULL) {
type = new ArrayType(pt->GetBaseType(), 0);
}
if (paramNames[i] != "")
ret += paramNames[i];
else
FATAL("Exporting a polymorphic function with incomplete arguments");
if (i != paramTypes.size() - 1)
ret += ", ";
}
ret += ")";
return ret;
}
std::string
FunctionType::GetCDeclarationForDispatch(const std::string &fname) const {
@@ -4041,7 +4078,8 @@ bool
Type::IsBasicType(const Type *type) {
return (CastType<AtomicType>(type) != NULL ||
CastType<EnumType>(type) != NULL ||
CastType<PointerType>(type) != NULL);
CastType<PointerType>(type) != NULL ||
CastType<PolyType>(type) != NULL);
}

1
type.h
View File

@@ -988,6 +988,7 @@ public:
std::string GetString() const;
std::string Mangle() const;
std::string GetCDeclaration(const std::string &fname) const;
std::string GetCCall(const std::string &fname) const;
std::string GetCDeclarationForDispatch(const std::string &fname) const;
llvm::Type *LLVMType(llvm::LLVMContext *ctx) const;