Skip to content

Commit 4d50e89

Browse files
committed
[CIR][CIRGen][Dialect] Use the correct alloca address space
1 parent f78f9a5 commit 4d50e89

File tree

8 files changed

+36
-17
lines changed

8 files changed

+36
-17
lines changed

clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h

+10
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ class CIRDataLayout {
100100
getPointerTypeSizeInBits(Ty), false);
101101
return IntTy;
102102
}
103+
104+
unsigned getAllocaMemorySpace() const {
105+
mlir::Attribute memorySpaceAttr = layout.getAllocaMemorySpace();
106+
// The empty attribute represents the default address space (0)
107+
if (!memorySpaceAttr)
108+
return 0;
109+
auto intAttr = mlir::dyn_cast<mlir::IntegerAttr>(memorySpaceAttr);
110+
assert(intAttr && "Expects integer attributes for memory space");
111+
return static_cast<unsigned>(intAttr.getUInt());
112+
}
103113
};
104114

105115
} // namespace cir

clang/include/clang/CIR/MissingFeatures.h

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ struct MissingFeatures {
3232
// Address space related
3333
static bool addressSpace() { return false; }
3434
static bool addressSpaceInGlobalVar() { return false; }
35+
static bool addressSpaceInAlloca() { return false; }
3536

3637
// Clang codegen options
3738
static bool strictVTablePointers() { return false; }

clang/lib/CIR/CodeGen/CIRGenBuilder.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -417,10 +417,12 @@ class CIRGenBuilderTy : public CIRBaseBuilderTy {
417417

418418
// Fetch the type representing a pointer to unsigned int values.
419419
mlir::cir::PointerType getUInt8PtrTy(unsigned AddrSpace = 0) {
420-
return typeCache.UInt8PtrTy;
420+
if (AddrSpace == 0)
421+
return typeCache.UInt8PtrTy;
422+
return mlir::cir::PointerType::get(getContext(), typeCache.UInt8Ty, AddrSpace);
421423
}
422424
mlir::cir::PointerType getUInt32PtrTy(unsigned AddrSpace = 0) {
423-
return mlir::cir::PointerType::get(getContext(), typeCache.UInt32Ty);
425+
return mlir::cir::PointerType::get(getContext(), typeCache.UInt32Ty, AddrSpace);
424426
}
425427

426428
/// Get a CIR anonymous struct type.

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -986,8 +986,8 @@ RValue CIRGenFunction::buildBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
986986
// pointer of type `void *`. This will require a change to the allocaOp
987987
// verifier.
988988
auto AllocaAddr = builder.createAlloca(
989-
getLoc(E->getSourceRange()), builder.getUInt8PtrTy(),
990-
builder.getUInt8Ty(), "bi_alloca", SuitableAlignmentInBytes, Size);
989+
getLoc(E->getSourceRange()), CGM.AllocaInt8PtrTy, builder.getUInt8Ty(),
990+
"bi_alloca", SuitableAlignmentInBytes, Size);
991991

992992
// Initialize the allocated buffer if required.
993993
if (BuiltinID != Builtin::BI__builtin_alloca_uninitialized)

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -2718,7 +2718,8 @@ mlir::Value CIRGenFunction::buildAlloca(StringRef name, mlir::Type ty,
27182718
mlir::Location loc, CharUnits alignment,
27192719
mlir::OpBuilder::InsertPoint ip,
27202720
mlir::Value arraySize) {
2721-
auto localVarPtrTy = mlir::cir::PointerType::get(builder.getContext(), ty);
2721+
auto localVarPtrTy = mlir::cir::PointerType::get(
2722+
builder.getContext(), ty, CGM.getDataLayout().getAllocaMemorySpace());
27222723
auto alignIntAttr = CGM.getSize(alignment);
27232724

27242725
mlir::Value addr;

clang/lib/CIR/CodeGen/CIRGenModule.cpp

+15-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "mlir/IR/OperationSupport.h"
3232
#include "mlir/IR/SymbolTable.h"
3333
#include "mlir/IR/Verifier.h"
34+
#include "mlir/Target/LLVMIR/Import.h"
3435
#include "clang/CIR/MissingFeatures.h"
3536

3637
#include "clang/AST/ASTConsumer.h"
@@ -96,6 +97,14 @@ static CIRGenCXXABI *createCXXABI(CIRGenModule &CGM) {
9697
}
9798
}
9899

100+
static void setMLIRDataLayout(mlir::ModuleOp &mod, const llvm::DataLayout &dl) {
101+
auto *context = mod.getContext();
102+
mod->setAttr(mlir::LLVM::LLVMDialect::getDataLayoutAttrName(),
103+
mlir::StringAttr::get(context, dl.getStringRepresentation()));
104+
mlir::DataLayoutSpecInterface dlSpec = mlir::translateDataLayout(dl, context);
105+
mod->setAttr(mlir::DLTIDialect::kDataLayoutAttrName, dlSpec);
106+
}
107+
99108
CIRGenModule::CIRGenModule(mlir::MLIRContext &context,
100109
clang::ASTContext &astctx,
101110
const clang::CodeGenOptions &CGO,
@@ -106,6 +115,10 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &context,
106115
target(astCtx.getTargetInfo()), ABI(createCXXABI(*this)), genTypes{*this},
107116
VTables{*this}, openMPRuntime(new CIRGenOpenMPRuntime(*this)) {
108117

118+
// Initialize DataLayout in the module op.
119+
auto layout = llvm::DataLayout(astCtx.getTargetInfo().getDataLayoutString());
120+
setMLIRDataLayout(theModule, layout);
121+
109122
// Initialize CIR signed integer types cache.
110123
SInt8Ty =
111124
::mlir::cir::IntType::get(builder.getContext(), 8, /*isSigned=*/true);
@@ -156,7 +169,8 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &context,
156169
/*isSigned=*/false);
157170
UInt8PtrTy = builder.getPointerTo(UInt8Ty);
158171
UInt8PtrPtrTy = builder.getPointerTo(UInt8PtrTy);
159-
AllocaInt8PtrTy = UInt8PtrTy;
172+
AllocaInt8PtrTy =
173+
builder.getPointerTo(UInt8Ty, getDataLayout().getAllocaMemorySpace());
160174
// TODO: GlobalsInt8PtrTy
161175
// TODO: ConstGlobalsPtrTy
162176
ASTAllocaAddressSpace = getTargetCIRGenInfo().getASTAllocaAddressSpace();

clang/lib/CIR/CodeGen/CIRGenerator.cpp

-11
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,6 @@ CIRGenerator::~CIRGenerator() {
4040
assert(DeferredInlineMemberFuncDefs.empty() || Diags.hasErrorOccurred());
4141
}
4242

43-
static void setMLIRDataLayout(mlir::ModuleOp &mod, const llvm::DataLayout &dl) {
44-
auto *context = mod.getContext();
45-
mod->setAttr(mlir::LLVM::LLVMDialect::getDataLayoutAttrName(),
46-
mlir::StringAttr::get(context, dl.getStringRepresentation()));
47-
mlir::DataLayoutSpecInterface dlSpec = mlir::translateDataLayout(dl, context);
48-
mod->setAttr(mlir::DLTIDialect::kDataLayoutAttrName, dlSpec);
49-
}
50-
5143
void CIRGenerator::Initialize(ASTContext &astCtx) {
5244
using namespace llvm;
5345

@@ -62,9 +54,6 @@ void CIRGenerator::Initialize(ASTContext &astCtx) {
6254
mlirCtx->getOrLoadDialect<mlir::omp::OpenMPDialect>();
6355
CGM = std::make_unique<CIRGenModule>(*mlirCtx.get(), astCtx, codeGenOpts,
6456
Diags);
65-
auto mod = CGM->getModule();
66-
auto layout = llvm::DataLayout(astCtx.getTargetInfo().getDataLayoutString());
67-
setMLIRDataLayout(mod, layout);
6857
}
6958

7059
bool CIRGenerator::verifyModule() { return CGM->verifyModule(); }

clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "clang/CIR/Dialect/IR/CIRDialect.h"
2222
#include "clang/CIR/Dialect/Passes.h"
2323
#include "clang/CIR/Interfaces/ASTAttrInterfaces.h"
24+
#include "clang/CIR/MissingFeatures.h"
2425
#include "llvm/ADT/SmallVector.h"
2526
#include "llvm/ADT/StringMap.h"
2627
#include "llvm/ADT/StringRef.h"
@@ -526,6 +527,7 @@ static void lowerArrayDtorCtorIntoLoop(CIRBaseBuilderTy &builder,
526527
mlir::Value end = builder.create<mlir::cir::PtrStrideOp>(
527528
loc, eltTy, begin, numArrayElementsConst);
528529

530+
assert(!::cir::MissingFeatures::addressSpaceInAlloca());
529531
auto tmpAddr = builder.createAlloca(
530532
loc, /*addr type*/ builder.getPointerTo(eltTy),
531533
/*var type*/ eltTy, "__array_idx", clang::CharUnits::One());

0 commit comments

Comments
 (0)