Skip to content

Commit

Permalink
[CIR][CIRGen] Support annotations on local var decl
Browse files Browse the repository at this point in the history
LLVM lowering support coming next.
  • Loading branch information
bcardosolopes committed Oct 9, 2024
1 parent 4b0a36d commit 93f58cc
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 12 deletions.
2 changes: 2 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ def AllocaOp : CIR_Op<"alloca", [
StrAttr:$name,
UnitAttr:$init,
ConfinedAttr<OptionalAttr<I64Attr>, [IntMinValue<0>]>:$alignment,
OptionalAttr<ArrayAttr>:$annotations,
OptionalAttr<ASTVarDeclInterface>:$ast
);

Expand Down Expand Up @@ -530,6 +531,7 @@ def AllocaOp : CIR_Op<"alloca", [
`[` $name
(`,` `init` $init^)?
`]`
($annotations^)?
(`ast` $ast^)? attr-dict
}];

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CIR/CodeGen/CIRGenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ CIRGenFunction::buildAutoVarAlloca(const VarDecl &D,
// Emit debug info for local var declaration.
assert(!MissingFeatures::generateDebugInfo());

if (D.hasAttr<AnnotateAttr>() && HaveInsertPoint())
if (D.hasAttr<AnnotateAttr>())
buildVarAnnotations(&D, address.emitRawPointer());

// TODO(cir): in LLVM this calls @llvm.lifetime.end.
Expand Down
8 changes: 6 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1892,7 +1892,11 @@ mlir::Value CIRGenFunction::buildAlignmentAssumption(

void CIRGenFunction::buildVarAnnotations(const VarDecl *decl, mlir::Value val) {
assert(decl->hasAttr<AnnotateAttr>() && "no annotate attribute");
for ([[maybe_unused]] const auto *I : decl->specific_attrs<AnnotateAttr>()) {
llvm_unreachable("NYI");
llvm::SmallVector<mlir::Attribute, 4> annotations;
for (const auto *annot : decl->specific_attrs<AnnotateAttr>()) {
annotations.push_back(CGM.buildAnnotateAttr(annot));
}
auto allocaOp = dyn_cast_or_null<mlir::cir::AllocaOp>(val.getDefiningOp());
assert(allocaOp && "expects available alloca");
allocaOp.setAnnotationsAttr(builder.getArrayAttr(annotations));
}
4 changes: 2 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3350,7 +3350,7 @@ LangAS CIRGenModule::getGlobalVarAddressSpace(const VarDecl *D) {
return getTargetCIRGenInfo().getGlobalVarAddressSpace(*this, D);
}

mlir::ArrayAttr CIRGenModule::buildAnnotationArgs(AnnotateAttr *attr) {
mlir::ArrayAttr CIRGenModule::buildAnnotationArgs(const AnnotateAttr *attr) {
ArrayRef<Expr *> exprs = {attr->args_begin(), attr->args_size()};
if (exprs.empty()) {
return mlir::ArrayAttr::get(builder.getContext(), {});
Expand Down Expand Up @@ -3392,7 +3392,7 @@ mlir::ArrayAttr CIRGenModule::buildAnnotationArgs(AnnotateAttr *attr) {
}

mlir::cir::AnnotationAttr
CIRGenModule::buildAnnotateAttr(clang::AnnotateAttr *aa) {
CIRGenModule::buildAnnotateAttr(const clang::AnnotateAttr *aa) {
mlir::StringAttr annoGV = builder.getStringAttr(aa->getAnnotation());
mlir::ArrayAttr args = buildAnnotationArgs(aa);
return mlir::cir::AnnotationAttr::get(builder.getContext(), annoGV, args);
Expand Down
14 changes: 7 additions & 7 deletions clang/lib/CIR/CodeGen/CIRGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,12 @@ class CIRGenModule : public CIRGenTypeCache {
/// Emits OpenCL specific Metadata e.g. OpenCL version.
void buildOpenCLMetadata();

/// Create cir::AnnotationAttr which contains the annotation
/// information for a given GlobalValue. Notice that a GlobalValue could
/// have multiple annotations, and this function creates attribute for
/// one of them.
mlir::cir::AnnotationAttr buildAnnotateAttr(const clang::AnnotateAttr *aa);

private:
// An ordered map of canonical GlobalDecls to their mangled names.
llvm::MapVector<clang::GlobalDecl, llvm::StringRef> MangledDeclNames;
Expand All @@ -817,13 +823,7 @@ class CIRGenModule : public CIRGenTypeCache {
void buildGlobalAnnotations();

/// Emit additional args of the annotation.
mlir::ArrayAttr buildAnnotationArgs(clang::AnnotateAttr *attr);

/// Create cir::AnnotationAttr which contains the annotation
/// information for a given GlobalValue. Notice that a GlobalValue could
/// have multiple annotations, and this function creates attribute for
/// one of them.
mlir::cir::AnnotationAttr buildAnnotateAttr(clang::AnnotateAttr *aa);
mlir::ArrayAttr buildAnnotationArgs(const clang::AnnotateAttr *attr);

/// Add global annotations for a global value.
/// Those annotations are emitted during lowering to the LLVM code.
Expand Down
8 changes: 8 additions & 0 deletions clang/test/CIR/CodeGen/annotations-var.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// RUN: %clang_cc1 -triple aarch64-none-linux-android21 -fclangir -emit-cir %s -o %t.cir
// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR

void local(void) {
int localvar __attribute__((annotate("localvar_ann_0"))) __attribute__((annotate("localvar_ann_1"))) = 3;
// CIR-LABEL: @local
// CIR: %0 = cir.alloca !s32i, !cir.ptr<!s32i>, ["localvar", init] [#cir.annotation<name = "localvar_ann_0", args = []>, #cir.annotation<name = "localvar_ann_1", args = []>]
}

0 comments on commit 93f58cc

Please sign in to comment.