Skip to content

Commit b14799d

Browse files
gitoleglanza
authored andcommitted
[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 1bb157c commit b14799d

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
@@ -2287,17 +2287,19 @@ mlir::Value CIRGenFunction::buildOpOnBoolExpr(const Expr *cond,
22872287

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

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

@@ -2307,7 +2309,7 @@ mlir::Value CIRGenFunction::buildAlloca(StringRef name, mlir::Type ty,
23072309
builder.restoreInsertionPoint(ip);
23082310
addr = builder.create<mlir::cir::AllocaOp>(loc, /*addr type*/ localVarPtrTy,
23092311
/*var type*/ ty, name,
2310-
alignIntAttr);
2312+
alignIntAttr, arraySize);
23112313
if (currVarDecl) {
23122314
auto alloca = cast<mlir::cir::AllocaOp>(addr.getDefiningOp());
23132315
alloca.setAstAttr(ASTVarDeclAttr::get(builder.getContext(), currVarDecl));
@@ -2318,9 +2320,10 @@ mlir::Value CIRGenFunction::buildAlloca(StringRef name, mlir::Type ty,
23182320

23192321
mlir::Value CIRGenFunction::buildAlloca(StringRef name, QualType ty,
23202322
mlir::Location loc, CharUnits alignment,
2321-
bool insertIntoFnEntryBlock) {
2323+
bool insertIntoFnEntryBlock,
2324+
mlir::Value arraySize) {
23222325
return buildAlloca(name, getCIRType(ty), loc, alignment,
2323-
insertIntoFnEntryBlock);
2326+
insertIntoFnEntryBlock, arraySize);
23242327
}
23252328

23262329
mlir::Value CIRGenFunction::buildLoadOfScalar(LValue lvalue,
@@ -2468,12 +2471,11 @@ Address CIRGenFunction::CreateMemTemp(QualType Ty, CharUnits Align,
24682471

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

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)