Skip to content

Commit 93f58cc

Browse files
committed
[CIR][CIRGen] Support annotations on local var decl
LLVM lowering support coming next.
1 parent 4b0a36d commit 93f58cc

File tree

6 files changed

+26
-12
lines changed

6 files changed

+26
-12
lines changed

clang/include/clang/CIR/Dialect/IR/CIROps.td

+2
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ def AllocaOp : CIR_Op<"alloca", [
493493
StrAttr:$name,
494494
UnitAttr:$init,
495495
ConfinedAttr<OptionalAttr<I64Attr>, [IntMinValue<0>]>:$alignment,
496+
OptionalAttr<ArrayAttr>:$annotations,
496497
OptionalAttr<ASTVarDeclInterface>:$ast
497498
);
498499

@@ -530,6 +531,7 @@ def AllocaOp : CIR_Op<"alloca", [
530531
`[` $name
531532
(`,` `init` $init^)?
532533
`]`
534+
($annotations^)?
533535
(`ast` $ast^)? attr-dict
534536
}];
535537

clang/lib/CIR/CodeGen/CIRGenDecl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ CIRGenFunction::buildAutoVarAlloca(const VarDecl &D,
198198
// Emit debug info for local var declaration.
199199
assert(!MissingFeatures::generateDebugInfo());
200200

201-
if (D.hasAttr<AnnotateAttr>() && HaveInsertPoint())
201+
if (D.hasAttr<AnnotateAttr>())
202202
buildVarAnnotations(&D, address.emitRawPointer());
203203

204204
// TODO(cir): in LLVM this calls @llvm.lifetime.end.

clang/lib/CIR/CodeGen/CIRGenFunction.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -1892,7 +1892,11 @@ mlir::Value CIRGenFunction::buildAlignmentAssumption(
18921892

18931893
void CIRGenFunction::buildVarAnnotations(const VarDecl *decl, mlir::Value val) {
18941894
assert(decl->hasAttr<AnnotateAttr>() && "no annotate attribute");
1895-
for ([[maybe_unused]] const auto *I : decl->specific_attrs<AnnotateAttr>()) {
1896-
llvm_unreachable("NYI");
1895+
llvm::SmallVector<mlir::Attribute, 4> annotations;
1896+
for (const auto *annot : decl->specific_attrs<AnnotateAttr>()) {
1897+
annotations.push_back(CGM.buildAnnotateAttr(annot));
18971898
}
1899+
auto allocaOp = dyn_cast_or_null<mlir::cir::AllocaOp>(val.getDefiningOp());
1900+
assert(allocaOp && "expects available alloca");
1901+
allocaOp.setAnnotationsAttr(builder.getArrayAttr(annotations));
18981902
}

clang/lib/CIR/CodeGen/CIRGenModule.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -3350,7 +3350,7 @@ LangAS CIRGenModule::getGlobalVarAddressSpace(const VarDecl *D) {
33503350
return getTargetCIRGenInfo().getGlobalVarAddressSpace(*this, D);
33513351
}
33523352

3353-
mlir::ArrayAttr CIRGenModule::buildAnnotationArgs(AnnotateAttr *attr) {
3353+
mlir::ArrayAttr CIRGenModule::buildAnnotationArgs(const AnnotateAttr *attr) {
33543354
ArrayRef<Expr *> exprs = {attr->args_begin(), attr->args_size()};
33553355
if (exprs.empty()) {
33563356
return mlir::ArrayAttr::get(builder.getContext(), {});
@@ -3392,7 +3392,7 @@ mlir::ArrayAttr CIRGenModule::buildAnnotationArgs(AnnotateAttr *attr) {
33923392
}
33933393

33943394
mlir::cir::AnnotationAttr
3395-
CIRGenModule::buildAnnotateAttr(clang::AnnotateAttr *aa) {
3395+
CIRGenModule::buildAnnotateAttr(const clang::AnnotateAttr *aa) {
33963396
mlir::StringAttr annoGV = builder.getStringAttr(aa->getAnnotation());
33973397
mlir::ArrayAttr args = buildAnnotationArgs(aa);
33983398
return mlir::cir::AnnotationAttr::get(builder.getContext(), annoGV, args);

clang/lib/CIR/CodeGen/CIRGenModule.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,12 @@ class CIRGenModule : public CIRGenTypeCache {
796796
/// Emits OpenCL specific Metadata e.g. OpenCL version.
797797
void buildOpenCLMetadata();
798798

799+
/// Create cir::AnnotationAttr which contains the annotation
800+
/// information for a given GlobalValue. Notice that a GlobalValue could
801+
/// have multiple annotations, and this function creates attribute for
802+
/// one of them.
803+
mlir::cir::AnnotationAttr buildAnnotateAttr(const clang::AnnotateAttr *aa);
804+
799805
private:
800806
// An ordered map of canonical GlobalDecls to their mangled names.
801807
llvm::MapVector<clang::GlobalDecl, llvm::StringRef> MangledDeclNames;
@@ -817,13 +823,7 @@ class CIRGenModule : public CIRGenTypeCache {
817823
void buildGlobalAnnotations();
818824

819825
/// Emit additional args of the annotation.
820-
mlir::ArrayAttr buildAnnotationArgs(clang::AnnotateAttr *attr);
821-
822-
/// Create cir::AnnotationAttr which contains the annotation
823-
/// information for a given GlobalValue. Notice that a GlobalValue could
824-
/// have multiple annotations, and this function creates attribute for
825-
/// one of them.
826-
mlir::cir::AnnotationAttr buildAnnotateAttr(clang::AnnotateAttr *aa);
826+
mlir::ArrayAttr buildAnnotationArgs(const clang::AnnotateAttr *attr);
827827

828828
/// Add global annotations for a global value.
829829
/// Those annotations are emitted during lowering to the LLVM code.
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %clang_cc1 -triple aarch64-none-linux-android21 -fclangir -emit-cir %s -o %t.cir
2+
// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
3+
4+
void local(void) {
5+
int localvar __attribute__((annotate("localvar_ann_0"))) __attribute__((annotate("localvar_ann_1"))) = 3;
6+
// CIR-LABEL: @local
7+
// CIR: %0 = cir.alloca !s32i, !cir.ptr<!s32i>, ["localvar", init] [#cir.annotation<name = "localvar_ann_0", args = []>, #cir.annotation<name = "localvar_ann_1", args = []>]
8+
}

0 commit comments

Comments
 (0)