Skip to content

Commit a673837

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 3449619 commit a673837

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

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

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

@@ -2308,7 +2310,7 @@ mlir::Value CIRGenFunction::buildAlloca(StringRef name, mlir::Type ty,
23082310
builder.restoreInsertionPoint(ip);
23092311
addr = builder.create<mlir::cir::AllocaOp>(loc, /*addr type*/ localVarPtrTy,
23102312
/*var type*/ ty, name,
2311-
alignIntAttr);
2313+
alignIntAttr, arraySize);
23122314
if (currVarDecl) {
23132315
auto alloca = cast<mlir::cir::AllocaOp>(addr.getDefiningOp());
23142316
alloca.setAstAttr(ASTVarDeclAttr::get(builder.getContext(), currVarDecl));
@@ -2319,9 +2321,10 @@ mlir::Value CIRGenFunction::buildAlloca(StringRef name, mlir::Type ty,
23192321

23202322
mlir::Value CIRGenFunction::buildAlloca(StringRef name, QualType ty,
23212323
mlir::Location loc, CharUnits alignment,
2322-
bool insertIntoFnEntryBlock) {
2324+
bool insertIntoFnEntryBlock,
2325+
mlir::Value arraySize) {
23232326
return buildAlloca(name, getCIRType(ty), loc, alignment,
2324-
insertIntoFnEntryBlock);
2327+
insertIntoFnEntryBlock, arraySize);
23252328
}
23262329

23272330
mlir::Value CIRGenFunction::buildLoadOfScalar(LValue lvalue,
@@ -2469,12 +2472,11 @@ Address CIRGenFunction::CreateMemTemp(QualType Ty, CharUnits Align,
24692472

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

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)