Skip to content

Commit d061557

Browse files
authored
Handles basic syntax using ext_vector_type type. (#179)
* 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.
1 parent f650eae commit d061557

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

tools/mlir-clang/Lib/clang-mlir.cc

+29-1
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,32 @@ mlir::Value MLIRScanner::createAllocOp(mlir::Type t, VarDecl *name,
453453
return alloc;
454454
}
455455

456+
ValueCategory
457+
MLIRScanner::VisitExtVectorElementExpr(clang::ExtVectorElementExpr *expr) {
458+
auto base = Visit(expr->getBase());
459+
SmallVector<uint32_t, 4> indices;
460+
expr->getEncodedElementAccess(indices);
461+
assert(indices.size() == 1 &&
462+
"The support for higher dimensions to be implemented.");
463+
auto idx = castToIndex(getMLIRLocation(expr->getAccessorLoc()),
464+
builder.create<ConstantIntOp>(loc, indices[0], 32));
465+
assert(base.isReference);
466+
base.isReference = false;
467+
auto mt = base.val.getType().cast<MemRefType>();
468+
auto shape = std::vector<int64_t>(mt.getShape());
469+
if (shape.size() == 1) {
470+
shape[0] = -1;
471+
} else {
472+
shape.erase(shape.begin());
473+
}
474+
auto mt0 =
475+
mlir::MemRefType::get(shape, mt.getElementType(),
476+
MemRefLayoutAttrInterface(), mt.getMemorySpace());
477+
base.val = builder.create<polygeist::SubIndexOp>(loc, mt0, base.val,
478+
getConstantIndex(0));
479+
return CommonArrayLookup(base, idx, base.isReference);
480+
}
481+
456482
ValueCategory MLIRScanner::VisitConstantExpr(clang::ConstantExpr *expr) {
457483
auto sv = Visit(expr->getSubExpr());
458484
if (auto ty = getMLIRType(expr->getType()).dyn_cast<mlir::IntegerType>()) {
@@ -4237,9 +4263,11 @@ MLIRASTConsumer::GetOrCreateGlobal(const ValueDecl *FD, std::string prefix,
42374263
auto rt = getMLIRType(FD->getType());
42384264
unsigned memspace = 0;
42394265
bool isArray = isa<clang::ArrayType>(FD->getType());
4266+
bool isExtVectorType =
4267+
isa<clang::ExtVectorType>(FD->getType()->getUnqualifiedDesugaredType());
42404268

42414269
mlir::MemRefType mr;
4242-
if (!isArray) {
4270+
if (!isArray && !isExtVectorType) {
42434271
mr = mlir::MemRefType::get(1, rt, {}, memspace);
42444272
} else {
42454273
auto mt = rt.cast<mlir::MemRefType>();

tools/mlir-clang/Lib/clang-mlir.h

+2
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ class MLIRScanner : public StmtVisitor<MLIRScanner, ValueCategory> {
235235

236236
ValueCategory VisitImplicitValueInitExpr(clang::ImplicitValueInitExpr *decl);
237237

238+
ValueCategory VisitExtVectorElementExpr(clang::ExtVectorElementExpr *expr);
239+
238240
ValueCategory VisitConstantExpr(clang::ConstantExpr *expr);
239241

240242
ValueCategory VisitAtomicExpr(clang::AtomicExpr *expr);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: mlir-clang %s --function=* -S | FileCheck %s
2+
3+
typedef size_t size_t_vec __attribute__((ext_vector_type(3)));
4+
5+
size_t evt() {
6+
size_t_vec stv;
7+
return stv.x;
8+
}
9+
10+
extern "C" const size_t_vec stv;
11+
size_t evt2() {
12+
return stv.x;
13+
}
14+
15+
// CHECK: func.func @_Z3evtv() -> i32 attributes {llvm.linkage = #llvm.linkage<external>} {
16+
// CHECK-NEXT: %0 = memref.alloca() : memref<1x3xi32>
17+
// CHECK-NEXT: %1 = affine.load %0[0, 0] : memref<1x3xi32>
18+
// CHECK-NEXT: return %1 : i32
19+
// CHECK-NEXT: }
20+
// CHECK: func.func @_Z4evt2v() -> i32 attributes {llvm.linkage = #llvm.linkage<external>} {
21+
// CHECK-NEXT: %0 = memref.get_global @stv : memref<3xi32>
22+
// CHECK-NEXT: %1 = affine.load %0[0] : memref<3xi32>
23+
// CHECK-NEXT: return %1 : i32
24+
// CHECK-NEXT: }

0 commit comments

Comments
 (0)