Correct pair behavior

This commit is contained in:
William S. Moses 2021-08-18 13:51:25 -04:00 committed by William Moses
parent 020886beda
commit 8fb325d427
7 changed files with 121 additions and 43 deletions

@ -1 +1 @@
Subproject commit 8c50b5fbfef113157d35f74b95c959c20eb37fab
Subproject commit 8c2ff7b69e77a9eccf4a13bcc21dee5894b251af

View File

@ -108,7 +108,7 @@ ValueWithOffsets MLIRScanner::VisitDeclStmt(clang::DeclStmt *decl) {
for (auto sub : decl->decls()) {
if (auto vd = dyn_cast<VarDecl>(sub)) {
VisitVarDecl(vd);
} else if (isa<TypeAliasDecl, RecordDecl, StaticAssertDecl, TypedefDecl>(
} else if (isa<TypeAliasDecl, RecordDecl, StaticAssertDecl, TypedefDecl, UsingDecl>(
sub)) {
} else {
llvm::errs() << " + visiting unknonwn sub decl stmt\n";
@ -230,6 +230,12 @@ ValueWithOffsets MLIRScanner::VisitVarDecl(clang::VarDecl *decl) {
LLVMABI = true;
else
Glob.getMLIRType(decl->getType(), &isArray);
if (!LLVMABI && isArray) {
subType =
Glob.getMLIRType(Glob.CGM.getContext().getPointerType(decl->getType()));
isArray = true;
}
if (auto init = decl->getInit()) {
if (auto CE = dyn_cast<CXXConstructExpr>(init)) {
@ -2025,11 +2031,8 @@ ValueWithOffsets MLIRScanner::VisitCallExpr(clang::CallExpr *expr) {
loc, size,
builder.create<mlir::ConstantIndexOp>(loc, elemSize));
std::vector<mlir::Value> start = {getConstantIndex(0)};
std::vector<mlir::Value> sizes = {size};
AffineMap map = builder.getSymbolIdentityMap();
auto affineOp =
builder.create<AffineForOp>(loc, start, map, sizes, map);
builder.create<scf::ForOp>(loc, getConstantIndex(0), size, getConstantIndex(1));
auto oldpoint = builder.getInsertionPoint();
auto oldblock = builder.getInsertionBlock();
@ -2045,17 +2048,15 @@ ValueWithOffsets MLIRScanner::VisitCallExpr(clang::CallExpr *expr) {
Glob.CGM.getContext().getPointerType(elem))
.cast<MemRefType>();
auto shape = std::vector<int64_t>(mt.getShape());
std::vector<mlir::Value> sizes = {getConstantIndex(shape[1])};
AffineMap map = builder.getSymbolIdentityMap();
auto affineOp =
builder.create<AffineForOp>(loc, start, map, sizes, map);
builder.create<scf::ForOp>(loc, getConstantIndex(0), getConstantIndex(shape[1]), getConstantIndex(1));
args.push_back(affineOp.getInductionVar());
builder.setInsertionPointToStart(
&affineOp.getLoopBody().front());
}
builder.create<AffineStoreOp>(
loc, builder.create<AffineLoadOp>(loc, src, args), dst,
builder.create<memref::StoreOp>(
loc, builder.create<memref::LoadOp>(loc, src, args), dst,
args);
// TODO: set the value of the iteration value to the final bound
@ -2156,6 +2157,14 @@ ValueWithOffsets MLIRScanner::VisitCallExpr(clang::CallExpr *expr) {
if (i == 0) {
tostore = Visit(a).getValue(builder);
auto mt = tostore.getType().cast<MemRefType>();
auto shape = std::vector<int64_t>(mt.getShape());
mlir::Value res;
shape.erase(shape.begin());
auto mt0 = mlir::MemRefType::get(shape, mt.getElementType(),
mt.getAffineMaps(), mt.getMemorySpace());
tostore = builder.create<polygeist::SubIndexOp>(loc, mt0, tostore,
getConstantIndex(0));
i++;
auto indexType = mlir::IntegerType::get(module.getContext(), 64);
auto one = builder.create<mlir::ConstantOp>(
@ -3438,6 +3447,7 @@ ValueWithOffsets MLIRScanner::CommonFieldLookup(clang::QualType CT,
ValueWithOffsets MLIRScanner::VisitDeclRefExpr(DeclRefExpr *E) {
auto name = E->getDecl()->getName().str();
assert(!isa<FunctionDecl>(E->getDecl()));
if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
if (Captures.find(VD) != Captures.end()) {
@ -3584,6 +3594,7 @@ ValueWithOffsets MLIRScanner::VisitCastExpr(CastExpr *E) {
case clang::CastKind::CK_UserDefinedConversion: {
return Visit(E->getSubExpr());
}
case clang::CastKind::CK_BaseToDerived:
case clang::CastKind::CK_DerivedToBase:
case clang::CastKind::CK_UncheckedDerivedToBase: {
auto se = Visit(E->getSubExpr());
@ -3956,6 +3967,7 @@ ValueWithOffsets MLIRScanner::VisitCastExpr(CastExpr *E) {
return ValueWithOffsets(res, /*isReference*/ false);
}
default:
EmittingFunctionDecl->dump();
E->dump();
assert(0 && "unhandled cast");
}
@ -4452,6 +4464,8 @@ mlir::Value MLIRASTConsumer::GetOrCreateGlobalLLVMString(
}
mlir::FuncOp MLIRASTConsumer::GetOrCreateMLIRFunction(const FunctionDecl *FD) {
assert(FD->getTemplatedKind() != FunctionDecl::TemplatedKind::TK_FunctionTemplate);
assert(FD->getTemplatedKind() != FunctionDecl::TemplatedKind::TK_DependentFunctionTemplateSpecialization);
std::string name;
if (auto CC = dyn_cast<CXXConstructorDecl>(FD))
name = CGM.getMangledName(GlobalDecl(CC, CXXCtorType::Ctor_Complete)).str();
@ -4559,11 +4573,12 @@ mlir::FuncOp MLIRASTConsumer::GetOrCreateMLIRFunction(const FunctionDecl *FD) {
functions[name] = function;
module.push_back(function);
const FunctionDecl *Def = nullptr;
if (FD->isDefined(Def, /*checkforfriend*/ true) &&
Def->getTemplatedKind() != FunctionDecl::TK_FunctionTemplate)
if (FD->isDefined(Def, /*checkforfriend*/ true) && Def->getBody()) {
assert(Def->getTemplatedKind() != FunctionDecl::TemplatedKind::TK_FunctionTemplate);
assert(Def->getTemplatedKind() != FunctionDecl::TemplatedKind::TK_DependentFunctionTemplateSpecialization);
functionsToEmit.push_back(Def);
else if (FD->getIdentifier())
emitIfFound.insert(FD->getName().str());
} else if (FD->getIdentifier())
emitIfFound.insert(name);
/*else {
for (const FunctionDecl *s_FD : FD->redecls()) {
s_FD->dump();
@ -4577,9 +4592,10 @@ mlir::FuncOp MLIRASTConsumer::GetOrCreateMLIRFunction(const FunctionDecl *FD) {
void MLIRASTConsumer::run() {
while (functionsToEmit.size()) {
const FunctionDecl *FD = functionsToEmit.front();
assert(FD->getBody());
functionsToEmit.pop_front();
assert(FD->getTemplatedKind() != FunctionDecl::TK_FunctionTemplate);
assert(FD->getTemplatedKind() != FunctionDecl::TemplatedKind::TK_DependentFunctionTemplateSpecialization);
std::string name;
if (auto CC = dyn_cast<CXXConstructorDecl>(FD))
name =
@ -4608,11 +4624,19 @@ void MLIRASTConsumer::HandleDeclContext(DeclContext *DC) {
continue;
if (fd->getIdentifier() == nullptr)
continue;
std::string name;
if (auto CC = dyn_cast<CXXConstructorDecl>(fd))
name = CGM.getMangledName(GlobalDecl(CC, CXXCtorType::Ctor_Complete)).str();
else
name = CGM.getMangledName(fd).str();
if ((emitIfFound.count("*") && fd->getName() != "fpclassify" &&
!fd->isStatic()) ||
emitIfFound.count(fd->getName().str())) {
if (fd->getTemplatedKind() != FunctionDecl::TK_FunctionTemplate)
emitIfFound.count(name)) {
if (fd->getTemplatedKind() != FunctionDecl::TK_FunctionTemplate) {
functionsToEmit.push_back(fd);
}
} else {
}
}
@ -4636,11 +4660,19 @@ bool MLIRASTConsumer::HandleTopLevelDecl(DeclGroupRef dg) {
continue;
if (fd->getIdentifier() == nullptr)
continue;
std::string name;
if (auto CC = dyn_cast<CXXConstructorDecl>(fd))
name = CGM.getMangledName(GlobalDecl(CC, CXXCtorType::Ctor_Complete)).str();
else
name = CGM.getMangledName(fd).str();
if ((emitIfFound.count("*") && fd->getName() != "fpclassify" &&
!fd->isStatic()) ||
emitIfFound.count(fd->getName().str())) {
if (fd->getTemplatedKind() != FunctionDecl::TK_FunctionTemplate)
emitIfFound.count(name)) {
if (fd->getTemplatedKind() != FunctionDecl::TK_FunctionTemplate) {
functionsToEmit.push_back(fd);
}
} else {
}
}

View File

@ -131,6 +131,10 @@ struct ValueWithOffsets {
if (auto mt = val.getType().dyn_cast<MemRefType>()) {
assert(smt.getElementType() == mt.getElementType());
if (mt.getShape().size() != smt.getShape().size()) {
llvm::errs() << " val: " << val << " tsv: " << toStore.val << "\n";
llvm::errs() << " mt: " << mt << " smt: " << smt << "\n";
}
assert(mt.getShape().size() == smt.getShape().size());
assert(smt.getShape().back() == mt.getShape().back());
@ -563,6 +567,7 @@ public:
}
Stmt *stmt = fd->getBody();
assert(stmt);
if (ShowAST) {
stmt->dump();
}

View File

@ -8,21 +8,21 @@ double alloc() {
return Tp.tv_sec + Tp.tv_usec * 1.0e-6;
}
; CHECK: func @alloc() -> f64 {
; CHECK-NEXT: %cst = constant 9.9999999999999995E-7 : f64
; CHECK-NEXT: %c1_i32 = constant 1 : i32
; CHECK-NEXT: %c0_i32 = constant 0 : i32
; CHECK-NEXT: %c1_i64 = constant 1 : i64
; CHECK-NEXT: %0 = llvm.alloca %c1_i64 x !llvm.struct<(i64, i64)> : (i64) -> !llvm.ptr<struct<(i64, i64)>>
; CHECK-NEXT: %1 = llvm.mlir.null : !llvm.ptr<struct<(i32, i32)>>
; CHECK-NEXT: %2 = llvm.call @gettimeofday(%0, %1) : (!llvm.ptr<struct<(i64, i64)>>, !llvm.ptr<struct<(i32, i32)>>) -> i32
; CHECK-NEXT: %3 = llvm.getelementptr %0[%c0_i32, %c0_i32] : (!llvm.ptr<struct<(i64, i64)>>, i32, i32) -> !llvm.ptr<i64>
; CHECK-NEXT: %4 = llvm.load %3 : !llvm.ptr<i64>
; CHECK-NEXT: %5 = llvm.getelementptr %0[%c0_i32, %c1_i32] : (!llvm.ptr<struct<(i64, i64)>>, i32, i32) -> !llvm.ptr<i64>
; CHECK-NEXT: %6 = llvm.load %5 : !llvm.ptr<i64>
; CHECK-NEXT: %7 = sitofp %4 : i64 to f64
; CHECK-NEXT: %8 = sitofp %6 : i64 to f64
; CHECK-NEXT: %9 = mulf %8, %cst : f64
; CHECK-NEXT: %10 = addf %7, %9 : f64
; CHECK-NEXT: return %10 : f64
; CHECK-NEXT: }
// CHECK: func @alloc() -> f64 {
// CHECK-NEXT: %cst = constant 9.9999999999999995E-7 : f64
// CHECK-NEXT: %c1_i32 = constant 1 : i32
// CHECK-NEXT: %c0_i32 = constant 0 : i32
// CHECK-NEXT: %c1_i64 = constant 1 : i64
// CHECK-NEXT: %0 = llvm.alloca %c1_i64 x !llvm.struct<(i64, i64)> : (i64) -> !llvm.ptr<struct<(i64, i64)>>
// CHECK-NEXT: %1 = llvm.mlir.null : !llvm.ptr<struct<(i32, i32)>>
// CHECK-NEXT: %2 = llvm.call @gettimeofday(%0, %1) : (!llvm.ptr<struct<(i64, i64)>>, !llvm.ptr<struct<(i32, i32)>>) -> i32
// CHECK-NEXT: %3 = llvm.getelementptr %0[%c0_i32, %c0_i32] : (!llvm.ptr<struct<(i64, i64)>>, i32, i32) -> !llvm.ptr<i64>
// CHECK-NEXT: %4 = llvm.load %3 : !llvm.ptr<i64>
// CHECK-NEXT: %5 = llvm.getelementptr %0[%c0_i32, %c1_i32] : (!llvm.ptr<struct<(i64, i64)>>, i32, i32) -> !llvm.ptr<i64>
// CHECK-NEXT: %6 = llvm.load %5 : !llvm.ptr<i64>
// CHECK-NEXT: %7 = sitofp %4 : i64 to f64
// CHECK-NEXT: %8 = sitofp %6 : i64 to f64
// CHECK-NEXT: %9 = mulf %8, %cst : f64
// CHECK-NEXT: %10 = addf %7, %9 : f64
// CHECK-NEXT: return %10 : f64
// CHECK-NEXT: }

View File

@ -0,0 +1,41 @@
// RUN: mlir-clang %s --function=create | FileCheck %s
typedef struct {
int a, b;
} pair;
pair byval(pair a, int x) {
a.b = x;
return a;
}
int create() {
pair p;
p.a = 0;
p.b = 1;
pair p2 = byval(p, 2);
return p2.a;
}
// CHECK: builtin.func @create() -> i32 {
// CHECK-NEXT: %c2_i32 = constant 2 : i32
// CHECK-NEXT: %c1_i32 = constant 1 : i32
// CHECK-NEXT: %c0_i32 = constant 0 : i32
// CHECK-NEXT: %0 = memref.alloca() : memref<1x2xi32>
// CHECK-NEXT: %1 = memref.alloca() : memref<1x2xi32>
// CHECK-NEXT: affine.store %c0_i32, %1[0, 0] : memref<1x2xi32>
// CHECK-NEXT: affine.store %c1_i32, %1[0, 1] : memref<1x2xi32>
// CHECK-NEXT: %2 = memref.cast %1 : memref<1x2xi32> to memref<?x2xi32>
// CHECK-NEXT: %3 = memref.cast %0 : memref<1x2xi32> to memref<?x2xi32>
// CHECK-NEXT: call @byval(%2, %c2_i32, %3) : (memref<?x2xi32>, i32, memref<?x2xi32>) -> ()
// CHECK-NEXT: %4 = affine.load %0[0, 0] : memref<1x2xi32>
// CHECK-NEXT: return %4 : i32
// CHECK-NEXT: }
// CHECK: builtin.func @byval(%arg0: memref<?x2xi32>, %arg1: i32, %arg2: memref<?x2xi32>) {
// CHECK-NEXT: affine.store %arg1, %arg0[0, 1] : memref<?x2xi32>
// CHECK-NEXT: %0 = affine.load %arg0[0, 0] : memref<?x2xi32>
// CHECK-NEXT: affine.store %0, %arg2[0, 0] : memref<?x2xi32>
// CHECK-NEXT: %1 = affine.load %arg0[0, 1] : memref<?x2xi32>
// CHECK-NEXT: affine.store %1, %arg2[0, 1] : memref<?x2xi32>
// CHECK-NEXT: return
// CHECK-NEXT: }

View File

@ -1,9 +1,9 @@
// RUN: split-file %s %t
// RUN: mlir-clang %t/matmul_signed_cmp.cpp --function=matmul --raise-scf-to-affine | FileCheck %s -check-prefix=GEMMSIGNED
// RUN: mlir-clang %t/matmul_unsigned_cmp.cpp --function=matmul_unsigned_cmp --raise-scf-to-affine | FileCheck %s -check-prefix=GEMMUNSIGNED
// RUN: mlir-clang %t/matmul_signed_cmp.c --function=matmul --raise-scf-to-affine | FileCheck %s -check-prefix=GEMMSIGNED
// RUN: mlir-clang %t/matmul_unsigned_cmp.c --function=matmul_unsigned_cmp --raise-scf-to-affine | FileCheck %s -check-prefix=GEMMUNSIGNED
//--- matmul_signed_cmp.cpp
//--- matmul_signed_cmp.c
#define N 200
#define M 300
#define K 400
@ -26,7 +26,7 @@ void matmul(DATA_TYPE A[N][K], DATA_TYPE B[K][M], DATA_TYPE C[N][M]) {
C[i][j] += A[i][k] * B[k][j];
}
//--- matmul_unsigned_cmp.cpp
//--- matmul_unsigned_cmp.c
void matmul_unsigned_cmp(float A[100][200], float B[200][300], float C[100][300]) {
int i, j, k;

View File

@ -257,7 +257,6 @@ int main(int argc, char **argv) {
module.dump();
return 4;
}
llvm::errs() << module << "\n";
mlir::PassManager pm3(&context);
pm3.addPass(mlir::createLowerToCFGPass());
pm3.addPass(createConvertOpenMPToLLVMPass());
@ -270,6 +269,7 @@ int main(int argc, char **argv) {
module.dump();
return 4;
}
module.dump();
} else {
if (mlir::failed(pm.run(module))) {