Skip to content

Commit

Permalink
[CIR][CIRGen] Support for section atttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
YazZz1k committed Feb 1, 2024
1 parent d3ef772 commit 38f7b58
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
3 changes: 2 additions & 1 deletion clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1410,7 +1410,8 @@ def GlobalOp : CIR_Op<"global", [Symbol, DeclareOpInterfaceMethods<RegionBranchO
OptionalAttr<AnyAttr>:$initial_value,
UnitAttr:$constant,
OptionalAttr<I64Attr>:$alignment,
OptionalAttr<ASTVarDeclInterface>:$ast
OptionalAttr<ASTVarDeclInterface>:$ast,
OptionalAttr<StrAttr>:$section
);
let regions = (region AnyRegion:$ctorRegion, AnyRegion:$dtorRegion);
let assemblyFormat = [{
Expand Down
11 changes: 4 additions & 7 deletions clang/lib/CIR/CodeGen/CIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -715,10 +715,8 @@ CIRGenModule::getOrCreateCIRGlobal(StringRef MangledName, mlir::Type Ty,

// Emit section information for extern variables.
if (D->hasExternalStorage()) {
if (const SectionAttr *SA = D->getAttr<SectionAttr>()) {
assert(!UnimplementedFeature::setGlobalVarSection());
llvm_unreachable("section info for extern vars is NYI");
}
if (const SectionAttr *SA = D->getAttr<SectionAttr>())
GV.setSectionAttr(builder.getStringAttr(SA->getName()));
}

// Handle XCore specific ABI requirements.
Expand Down Expand Up @@ -1018,9 +1016,8 @@ void CIRGenModule::buildGlobalVarDefinition(const clang::VarDecl *D,
// isTypeConstant(D->getType(), true));

// If it is in a read-only section, mark it 'constant'.
if (const SectionAttr *SA = D->getAttr<SectionAttr>()) {
assert(0 && "not implemented");
}
if (const SectionAttr *SA = D->getAttr<SectionAttr>())
GV.setSectionAttr(builder.getStringAttr(SA->getName()));

// TODO(cir):
// GV->setAlignment(getContext().getDeclAlign(D).getAsAlign());
Expand Down
1 change: 0 additions & 1 deletion clang/lib/CIR/CodeGen/UnimplementedFeatureGuarding.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ struct UnimplementedFeature {
// Unhandled global/linkage information.
static bool unnamedAddr() { return false; }
static bool setComdat() { return false; }
static bool setGlobalVarSection() { return false; }
static bool setDSOLocal() { return false; }
static bool threadLocal() { return false; }
static bool setDLLStorageClass() { return false; }
Expand Down
16 changes: 14 additions & 2 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1423,12 +1423,21 @@ class CIRGlobalOpLowering
const auto linkage = convertLinkage(op.getLinkage());
const auto symbol = op.getSymName();
const auto loc = op.getLoc();
std::optional<mlir::StringRef> section = op.getSection();
std::optional<mlir::Attribute> init = op.getInitialValue();

SmallVector<mlir::NamedAttribute> attributes;
if (section.has_value())
attributes.push_back(rewriter.getNamedAttr(
"section", rewriter.getStringAttr(section.value())));

// Check for missing funcionalities.
if (!init.has_value()) {
rewriter.replaceOpWithNewOp<mlir::LLVM::GlobalOp>(
op, llvmType, isConst, linkage, symbol, mlir::Attribute());
op, llvmType, isConst, linkage, symbol, mlir::Attribute(),
/*alignment*/ 0, /*addrSpace*/ 0,
/*dsoLocal*/ false, /*threadLocal*/ false,
/*comdat*/ mlir::SymbolRefAttr(), attributes);
return mlir::success();
}

Expand Down Expand Up @@ -1502,7 +1511,10 @@ class CIRGlobalOpLowering

// Rewrite op.
rewriter.replaceOpWithNewOp<mlir::LLVM::GlobalOp>(
op, llvmType, isConst, linkage, symbol, init.value());
op, llvmType, isConst, linkage, symbol, init.value(),
/*alignment*/ 0, /*addrSpace*/ 0,
/*dsoLocal*/ false, /*threadLocal*/ false,
/*comdat*/ mlir::SymbolRefAttr(), attributes);
return mlir::success();
}
};
Expand Down

0 comments on commit 38f7b58

Please sign in to comment.