Skip to content

Commit

Permalink
Handles basic syntax using ext_vector_type type. (#179)
Browse files Browse the repository at this point in the history
* Handles basic syntax using ext_vector_type type.
* Improvement is needed to support more complex syntax (such as ext.xyz)

* Refactors implementation of VisitExtVectorElementExpr using CommonArrayLookup.

* Adds test.
  • Loading branch information
mmoadeli authored May 18, 2022
1 parent f650eae commit d061557
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
30 changes: 29 additions & 1 deletion tools/mlir-clang/Lib/clang-mlir.cc
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,32 @@ mlir::Value MLIRScanner::createAllocOp(mlir::Type t, VarDecl *name,
return alloc;
}

ValueCategory
MLIRScanner::VisitExtVectorElementExpr(clang::ExtVectorElementExpr *expr) {
auto base = Visit(expr->getBase());
SmallVector<uint32_t, 4> indices;
expr->getEncodedElementAccess(indices);
assert(indices.size() == 1 &&
"The support for higher dimensions to be implemented.");
auto idx = castToIndex(getMLIRLocation(expr->getAccessorLoc()),
builder.create<ConstantIntOp>(loc, indices[0], 32));
assert(base.isReference);
base.isReference = false;
auto mt = base.val.getType().cast<MemRefType>();
auto shape = std::vector<int64_t>(mt.getShape());
if (shape.size() == 1) {
shape[0] = -1;
} else {
shape.erase(shape.begin());
}
auto mt0 =
mlir::MemRefType::get(shape, mt.getElementType(),
MemRefLayoutAttrInterface(), mt.getMemorySpace());
base.val = builder.create<polygeist::SubIndexOp>(loc, mt0, base.val,
getConstantIndex(0));
return CommonArrayLookup(base, idx, base.isReference);
}

ValueCategory MLIRScanner::VisitConstantExpr(clang::ConstantExpr *expr) {
auto sv = Visit(expr->getSubExpr());
if (auto ty = getMLIRType(expr->getType()).dyn_cast<mlir::IntegerType>()) {
Expand Down Expand Up @@ -4237,9 +4263,11 @@ MLIRASTConsumer::GetOrCreateGlobal(const ValueDecl *FD, std::string prefix,
auto rt = getMLIRType(FD->getType());
unsigned memspace = 0;
bool isArray = isa<clang::ArrayType>(FD->getType());
bool isExtVectorType =
isa<clang::ExtVectorType>(FD->getType()->getUnqualifiedDesugaredType());

mlir::MemRefType mr;
if (!isArray) {
if (!isArray && !isExtVectorType) {
mr = mlir::MemRefType::get(1, rt, {}, memspace);
} else {
auto mt = rt.cast<mlir::MemRefType>();
Expand Down
2 changes: 2 additions & 0 deletions tools/mlir-clang/Lib/clang-mlir.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ class MLIRScanner : public StmtVisitor<MLIRScanner, ValueCategory> {

ValueCategory VisitImplicitValueInitExpr(clang::ImplicitValueInitExpr *decl);

ValueCategory VisitExtVectorElementExpr(clang::ExtVectorElementExpr *expr);

ValueCategory VisitConstantExpr(clang::ConstantExpr *expr);

ValueCategory VisitAtomicExpr(clang::AtomicExpr *expr);
Expand Down
24 changes: 24 additions & 0 deletions tools/mlir-clang/Test/Verification/ext_vector_type.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// RUN: mlir-clang %s --function=* -S | FileCheck %s

typedef size_t size_t_vec __attribute__((ext_vector_type(3)));

size_t evt() {
size_t_vec stv;
return stv.x;
}

extern "C" const size_t_vec stv;
size_t evt2() {
return stv.x;
}

// CHECK: func.func @_Z3evtv() -> i32 attributes {llvm.linkage = #llvm.linkage<external>} {
// CHECK-NEXT: %0 = memref.alloca() : memref<1x3xi32>
// CHECK-NEXT: %1 = affine.load %0[0, 0] : memref<1x3xi32>
// CHECK-NEXT: return %1 : i32
// CHECK-NEXT: }
// CHECK: func.func @_Z4evt2v() -> i32 attributes {llvm.linkage = #llvm.linkage<external>} {
// CHECK-NEXT: %0 = memref.get_global @stv : memref<3xi32>
// CHECK-NEXT: %1 = affine.load %0[0] : memref<3xi32>
// CHECK-NEXT: return %1 : i32
// CHECK-NEXT: }

0 comments on commit d061557

Please sign in to comment.