Skip to content

Commit 01519e8

Browse files
authored
LLVM global initialization (#167)
* Add template member test * Add LLVM global initialization
1 parent dce289f commit 01519e8

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3746,9 +3746,6 @@ MLIRASTConsumer::GetOrCreateLLVMGlobal(const ValueDecl *FD,
37463746
break;
37473747
}
37483748

3749-
if (cast<VarDecl>(FD)->getInit())
3750-
cast<VarDecl>(FD)->getInit()->dump();
3751-
37523749
auto rt = getMLIRType(FD->getType());
37533750

37543751
mlir::OpBuilder builder(module->getContext());
@@ -3757,17 +3754,24 @@ MLIRASTConsumer::GetOrCreateLLVMGlobal(const ValueDecl *FD,
37573754
auto glob = builder.create<LLVM::GlobalOp>(
37583755
module->getLoc(), rt, /*constant*/ false, lnk, name, mlir::Attribute());
37593756

3760-
if (cast<VarDecl>(FD)->isThisDeclarationADefinition() ==
3757+
if (cast<VarDecl>(FD)->getInit() ||
3758+
cast<VarDecl>(FD)->isThisDeclarationADefinition() ==
37613759
VarDecl::Definition ||
37623760
cast<VarDecl>(FD)->isThisDeclarationADefinition() ==
37633761
VarDecl::TentativeDefinition) {
37643762
Block *blk = new Block();
37653763
glob.getInitializerRegion().push_back(blk);
37663764
builder.setInsertionPointToStart(blk);
3767-
builder.create<LLVM::ReturnOp>(
3768-
module->getLoc(),
3769-
std::vector<mlir::Value>(
3770-
{builder.create<LLVM::UndefOp>(module->getLoc(), rt)}));
3765+
mlir::Value res;
3766+
if (auto init = cast<VarDecl>(FD)->getInit()) {
3767+
MLIRScanner ms(*this, module, LTInfo);
3768+
ms.setEntryAndAllocBlock(blk);
3769+
res = ms.Visit(const_cast<Expr *>(init)).getValue(builder);
3770+
} else {
3771+
res = builder.create<LLVM::UndefOp>(module->getLoc(), rt);
3772+
}
3773+
builder.create<LLVM::ReturnOp>(module->getLoc(),
3774+
std::vector<mlir::Value>({res}));
37713775
}
37723776
return llvmGlobals[name] = glob;
37733777
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: mlir-clang %s --function=* -S | FileCheck %s
2+
3+
class House;
4+
5+
template <typename T>
6+
class Info;
7+
8+
template <>
9+
class Info<House>{
10+
public:
11+
static constexpr bool has_infinity = true;
12+
};
13+
14+
bool add_kernel_cuda() {
15+
return Info<House>::has_infinity;
16+
}
17+
18+
// CHECK: llvm.mlir.global external @_ZN4InfoI5HouseE12has_infinityE() : i8 {
19+
// CHECK-NEXT: %c1_i8 = arith.constant 1 : i8
20+
// CHECK-NEXT: llvm.return %c1_i8 : i8
21+
// CHECK-NEXT: }
22+
// CHECK: func @_Z15add_kernel_cudav() -> i8 attributes {llvm.linkage = #llvm.linkage<external>} {
23+
// CHECK-NEXT: %0 = llvm.mlir.addressof @_ZN4InfoI5HouseE12has_infinityE : !llvm.ptr<i8>
24+
// CHECK-NEXT: %1 = llvm.load %0 : !llvm.ptr<i8>
25+
// CHECK-NEXT: return %1 : i8
26+
// CHECK-NEXT: }

0 commit comments

Comments
 (0)