Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CIR][CodeGen] Enhance alloca helpers #367

Merged
merged 2 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 30 additions & 17 deletions clang/lib/CIR/CodeGen/CIRGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2286,17 +2286,19 @@ mlir::Value CIRGenFunction::buildOpOnBoolExpr(const Expr *cond,

mlir::Value CIRGenFunction::buildAlloca(StringRef name, mlir::Type ty,
mlir::Location loc, CharUnits alignment,
bool insertIntoFnEntryBlock) {
bool insertIntoFnEntryBlock,
mlir::Value arraySize) {
mlir::Block *entryBlock = insertIntoFnEntryBlock
? getCurFunctionEntryBlock()
: currLexScope->getEntryBlock();
return buildAlloca(name, ty, loc, alignment,
builder.getBestAllocaInsertPoint(entryBlock));
builder.getBestAllocaInsertPoint(entryBlock), arraySize);
}

mlir::Value CIRGenFunction::buildAlloca(StringRef name, mlir::Type ty,
mlir::Location loc, CharUnits alignment,
mlir::OpBuilder::InsertPoint ip) {
mlir::OpBuilder::InsertPoint ip,
mlir::Value arraySize) {
auto localVarPtrTy = mlir::cir::PointerType::get(builder.getContext(), ty);
auto alignIntAttr = CGM.getSize(alignment);

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

mlir::Value CIRGenFunction::buildAlloca(StringRef name, QualType ty,
mlir::Location loc, CharUnits alignment,
bool insertIntoFnEntryBlock) {
bool insertIntoFnEntryBlock,
mlir::Value arraySize) {
return buildAlloca(name, getCIRType(ty), loc, alignment,
insertIntoFnEntryBlock);
insertIntoFnEntryBlock, arraySize);
}

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

/// This creates a alloca and inserts it into the entry block of the
/// current region.
Address CIRGenFunction::CreateTempAllocaWithoutCast(mlir::Type Ty,
CharUnits Align,
mlir::Location Loc,
const Twine &Name,
mlir::Value ArraySize) {
auto Alloca = CreateTempAlloca(Ty, Loc, Name, ArraySize);
Address CIRGenFunction::CreateTempAllocaWithoutCast(
mlir::Type Ty, CharUnits Align, mlir::Location Loc, const Twine &Name,
mlir::Value ArraySize, mlir::OpBuilder::InsertPoint ip) {
auto Alloca = ip.isSet() ? CreateTempAlloca(Ty, Loc, Name, ip, ArraySize)
: CreateTempAlloca(Ty, Loc, Name, ArraySize);
Alloca.setAlignmentAttr(CGM.getSize(Align));
return Address(Alloca, Ty, Align);
}
Expand All @@ -2482,8 +2484,10 @@ Address CIRGenFunction::CreateTempAllocaWithoutCast(mlir::Type Ty,
Address CIRGenFunction::CreateTempAlloca(mlir::Type Ty, CharUnits Align,
mlir::Location Loc, const Twine &Name,
mlir::Value ArraySize,
Address *AllocaAddr) {
auto Alloca = CreateTempAllocaWithoutCast(Ty, Align, Loc, Name, ArraySize);
Address *AllocaAddr,
mlir::OpBuilder::InsertPoint ip) {
auto Alloca =
CreateTempAllocaWithoutCast(Ty, Align, Loc, Name, ArraySize, ip);
if (AllocaAddr)
*AllocaAddr = Alloca;
mlir::Value V = Alloca.getPointer();
Expand All @@ -2502,10 +2506,19 @@ mlir::cir::AllocaOp
CIRGenFunction::CreateTempAlloca(mlir::Type Ty, mlir::Location Loc,
const Twine &Name, mlir::Value ArraySize,
bool insertIntoFnEntryBlock) {
if (ArraySize)
assert(0 && "NYI");
return cast<mlir::cir::AllocaOp>(buildAlloca(Name.str(), Ty, Loc, CharUnits(),
insertIntoFnEntryBlock,
ArraySize)
.getDefiningOp());
}

/// This creates an alloca and inserts it into the provided insertion point
mlir::cir::AllocaOp CIRGenFunction::CreateTempAlloca(
mlir::Type Ty, mlir::Location Loc, const Twine &Name,
mlir::OpBuilder::InsertPoint ip, mlir::Value ArraySize) {
assert(ip.isSet() && "Insertion point is not set");
return cast<mlir::cir::AllocaOp>(
buildAlloca(Name.str(), Ty, Loc, CharUnits(), insertIntoFnEntryBlock)
buildAlloca(Name.str(), Ty, Loc, CharUnits(), ip, ArraySize)
.getDefiningOp());
}

Expand Down
19 changes: 14 additions & 5 deletions clang/lib/CIR/CodeGen/CIRGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,16 @@ class CIRGenFunction : public CIRGenTypeCache {
// FIXME(cir): move this to CIRGenBuider.h
mlir::Value buildAlloca(llvm::StringRef name, clang::QualType ty,
mlir::Location loc, clang::CharUnits alignment,
bool insertIntoFnEntryBlock = false);
bool insertIntoFnEntryBlock = false,
mlir::Value arraySize = nullptr);
mlir::Value buildAlloca(llvm::StringRef name, mlir::Type ty,
mlir::Location loc, clang::CharUnits alignment,
bool insertIntoFnEntryBlock = false);
bool insertIntoFnEntryBlock = false,
mlir::Value arraySize = nullptr);
mlir::Value buildAlloca(llvm::StringRef name, mlir::Type ty,
mlir::Location loc, clang::CharUnits alignment,
mlir::OpBuilder::InsertPoint ip);
mlir::OpBuilder::InsertPoint ip,
mlir::Value arraySize = nullptr);

private:
void buildAndUpdateRetAlloca(clang::QualType ty, mlir::Location loc,
Expand Down Expand Up @@ -1877,14 +1880,20 @@ class CIRGenFunction : public CIRGenTypeCache {
CreateTempAllocaInFnEntryBlock(mlir::Type Ty, mlir::Location Loc,
const Twine &Name = "tmp",
mlir::Value ArraySize = nullptr);
mlir::cir::AllocaOp CreateTempAlloca(mlir::Type Ty, mlir::Location Loc,
const Twine &Name = "tmp",
mlir::OpBuilder::InsertPoint ip = {},
mlir::Value ArraySize = nullptr);
Address CreateTempAlloca(mlir::Type Ty, CharUnits align, mlir::Location Loc,
const Twine &Name = "tmp",
mlir::Value ArraySize = nullptr,
Address *Alloca = nullptr);
Address *Alloca = nullptr,
mlir::OpBuilder::InsertPoint ip = {});
Address CreateTempAllocaWithoutCast(mlir::Type Ty, CharUnits align,
mlir::Location Loc,
const Twine &Name = "tmp",
mlir::Value ArraySize = nullptr);
mlir::Value ArraySize = nullptr,
mlir::OpBuilder::InsertPoint ip = {});

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