Skip to content

Commit 100d2bc

Browse files
authored
[CIR][CIRGen][NFC] Enhance alloca helpers (#367)
One more step towards variable length array support. This PR adds one more helper for the `alloca` instruction and re-use the existing ones. The reason is the following: right now there are two possible ways to insert alloca: either to a function entry block or to the given block after all the existing alloca instructions. But for VLA support we need to insert alloca anywhere, right after an array's size becomes known. Thus, we add one more parameter with the default value - insertion point. Also, we don't want copy-paste the code, and reuse the existing helpers, but it may be a little bit confusing to read.
1 parent fda287d commit 100d2bc

File tree

2 files changed

+44
-22
lines changed

2 files changed

+44
-22
lines changed

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2286,17 +2286,19 @@ mlir::Value CIRGenFunction::buildOpOnBoolExpr(const Expr *cond,
22862286

22872287
mlir::Value CIRGenFunction::buildAlloca(StringRef name, mlir::Type ty,
22882288
mlir::Location loc, CharUnits alignment,
2289-
bool insertIntoFnEntryBlock) {
2289+
bool insertIntoFnEntryBlock,
2290+
mlir::Value arraySize) {
22902291
mlir::Block *entryBlock = insertIntoFnEntryBlock
22912292
? getCurFunctionEntryBlock()
22922293
: currLexScope->getEntryBlock();
22932294
return buildAlloca(name, ty, loc, alignment,
2294-
builder.getBestAllocaInsertPoint(entryBlock));
2295+
builder.getBestAllocaInsertPoint(entryBlock), arraySize);
22952296
}
22962297

22972298
mlir::Value CIRGenFunction::buildAlloca(StringRef name, mlir::Type ty,
22982299
mlir::Location loc, CharUnits alignment,
2299-
mlir::OpBuilder::InsertPoint ip) {
2300+
mlir::OpBuilder::InsertPoint ip,
2301+
mlir::Value arraySize) {
23002302
auto localVarPtrTy = mlir::cir::PointerType::get(builder.getContext(), ty);
23012303
auto alignIntAttr = CGM.getSize(alignment);
23022304

@@ -2306,7 +2308,7 @@ mlir::Value CIRGenFunction::buildAlloca(StringRef name, mlir::Type ty,
23062308
builder.restoreInsertionPoint(ip);
23072309
addr = builder.create<mlir::cir::AllocaOp>(loc, /*addr type*/ localVarPtrTy,
23082310
/*var type*/ ty, name,
2309-
alignIntAttr);
2311+
alignIntAttr, arraySize);
23102312
if (currVarDecl) {
23112313
auto alloca = cast<mlir::cir::AllocaOp>(addr.getDefiningOp());
23122314
alloca.setAstAttr(ASTVarDeclAttr::get(builder.getContext(), currVarDecl));
@@ -2317,9 +2319,10 @@ mlir::Value CIRGenFunction::buildAlloca(StringRef name, mlir::Type ty,
23172319

23182320
mlir::Value CIRGenFunction::buildAlloca(StringRef name, QualType ty,
23192321
mlir::Location loc, CharUnits alignment,
2320-
bool insertIntoFnEntryBlock) {
2322+
bool insertIntoFnEntryBlock,
2323+
mlir::Value arraySize) {
23212324
return buildAlloca(name, getCIRType(ty), loc, alignment,
2322-
insertIntoFnEntryBlock);
2325+
insertIntoFnEntryBlock, arraySize);
23232326
}
23242327

23252328
mlir::Value CIRGenFunction::buildLoadOfScalar(LValue lvalue,
@@ -2467,12 +2470,11 @@ Address CIRGenFunction::CreateMemTemp(QualType Ty, CharUnits Align,
24672470

24682471
/// This creates a alloca and inserts it into the entry block of the
24692472
/// current region.
2470-
Address CIRGenFunction::CreateTempAllocaWithoutCast(mlir::Type Ty,
2471-
CharUnits Align,
2472-
mlir::Location Loc,
2473-
const Twine &Name,
2474-
mlir::Value ArraySize) {
2475-
auto Alloca = CreateTempAlloca(Ty, Loc, Name, ArraySize);
2473+
Address CIRGenFunction::CreateTempAllocaWithoutCast(
2474+
mlir::Type Ty, CharUnits Align, mlir::Location Loc, const Twine &Name,
2475+
mlir::Value ArraySize, mlir::OpBuilder::InsertPoint ip) {
2476+
auto Alloca = ip.isSet() ? CreateTempAlloca(Ty, Loc, Name, ip, ArraySize)
2477+
: CreateTempAlloca(Ty, Loc, Name, ArraySize);
24762478
Alloca.setAlignmentAttr(CGM.getSize(Align));
24772479
return Address(Alloca, Ty, Align);
24782480
}
@@ -2482,8 +2484,10 @@ Address CIRGenFunction::CreateTempAllocaWithoutCast(mlir::Type Ty,
24822484
Address CIRGenFunction::CreateTempAlloca(mlir::Type Ty, CharUnits Align,
24832485
mlir::Location Loc, const Twine &Name,
24842486
mlir::Value ArraySize,
2485-
Address *AllocaAddr) {
2486-
auto Alloca = CreateTempAllocaWithoutCast(Ty, Align, Loc, Name, ArraySize);
2487+
Address *AllocaAddr,
2488+
mlir::OpBuilder::InsertPoint ip) {
2489+
auto Alloca =
2490+
CreateTempAllocaWithoutCast(Ty, Align, Loc, Name, ArraySize, ip);
24872491
if (AllocaAddr)
24882492
*AllocaAddr = Alloca;
24892493
mlir::Value V = Alloca.getPointer();
@@ -2502,10 +2506,19 @@ mlir::cir::AllocaOp
25022506
CIRGenFunction::CreateTempAlloca(mlir::Type Ty, mlir::Location Loc,
25032507
const Twine &Name, mlir::Value ArraySize,
25042508
bool insertIntoFnEntryBlock) {
2505-
if (ArraySize)
2506-
assert(0 && "NYI");
2509+
return cast<mlir::cir::AllocaOp>(buildAlloca(Name.str(), Ty, Loc, CharUnits(),
2510+
insertIntoFnEntryBlock,
2511+
ArraySize)
2512+
.getDefiningOp());
2513+
}
2514+
2515+
/// This creates an alloca and inserts it into the provided insertion point
2516+
mlir::cir::AllocaOp CIRGenFunction::CreateTempAlloca(
2517+
mlir::Type Ty, mlir::Location Loc, const Twine &Name,
2518+
mlir::OpBuilder::InsertPoint ip, mlir::Value ArraySize) {
2519+
assert(ip.isSet() && "Insertion point is not set");
25072520
return cast<mlir::cir::AllocaOp>(
2508-
buildAlloca(Name.str(), Ty, Loc, CharUnits(), insertIntoFnEntryBlock)
2521+
buildAlloca(Name.str(), Ty, Loc, CharUnits(), ip, ArraySize)
25092522
.getDefiningOp());
25102523
}
25112524

clang/lib/CIR/CodeGen/CIRGenFunction.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,16 @@ class CIRGenFunction : public CIRGenTypeCache {
239239
// FIXME(cir): move this to CIRGenBuider.h
240240
mlir::Value buildAlloca(llvm::StringRef name, clang::QualType ty,
241241
mlir::Location loc, clang::CharUnits alignment,
242-
bool insertIntoFnEntryBlock = false);
242+
bool insertIntoFnEntryBlock = false,
243+
mlir::Value arraySize = nullptr);
243244
mlir::Value buildAlloca(llvm::StringRef name, mlir::Type ty,
244245
mlir::Location loc, clang::CharUnits alignment,
245-
bool insertIntoFnEntryBlock = false);
246+
bool insertIntoFnEntryBlock = false,
247+
mlir::Value arraySize = nullptr);
246248
mlir::Value buildAlloca(llvm::StringRef name, mlir::Type ty,
247249
mlir::Location loc, clang::CharUnits alignment,
248-
mlir::OpBuilder::InsertPoint ip);
250+
mlir::OpBuilder::InsertPoint ip,
251+
mlir::Value arraySize = nullptr);
249252

250253
private:
251254
void buildAndUpdateRetAlloca(clang::QualType ty, mlir::Location loc,
@@ -1877,14 +1880,20 @@ class CIRGenFunction : public CIRGenTypeCache {
18771880
CreateTempAllocaInFnEntryBlock(mlir::Type Ty, mlir::Location Loc,
18781881
const Twine &Name = "tmp",
18791882
mlir::Value ArraySize = nullptr);
1883+
mlir::cir::AllocaOp CreateTempAlloca(mlir::Type Ty, mlir::Location Loc,
1884+
const Twine &Name = "tmp",
1885+
mlir::OpBuilder::InsertPoint ip = {},
1886+
mlir::Value ArraySize = nullptr);
18801887
Address CreateTempAlloca(mlir::Type Ty, CharUnits align, mlir::Location Loc,
18811888
const Twine &Name = "tmp",
18821889
mlir::Value ArraySize = nullptr,
1883-
Address *Alloca = nullptr);
1890+
Address *Alloca = nullptr,
1891+
mlir::OpBuilder::InsertPoint ip = {});
18841892
Address CreateTempAllocaWithoutCast(mlir::Type Ty, CharUnits align,
18851893
mlir::Location Loc,
18861894
const Twine &Name = "tmp",
1887-
mlir::Value ArraySize = nullptr);
1895+
mlir::Value ArraySize = nullptr,
1896+
mlir::OpBuilder::InsertPoint ip = {});
18881897

18891898
/// Create a temporary memory object of the given type, with
18901899
/// appropriate alignmen and cast it to the default address space. Returns

0 commit comments

Comments
 (0)