From 73f84e8cfa36743083af2a7c8f1cb12796521542 Mon Sep 17 00:00:00 2001 From: Bruno Cardoso Lopes Date: Wed, 9 Oct 2024 13:21:46 -0700 Subject: [PATCH] [CIR][CIRGen] Support annotations on local var decl LLVM lowering support coming next. --- clang/include/clang/CIR/Dialect/IR/CIROps.td | 2 ++ clang/lib/CIR/CodeGen/CIRGenDecl.cpp | 2 +- clang/lib/CIR/CodeGen/CIRGenFunction.cpp | 8 ++++++-- clang/lib/CIR/CodeGen/CIRGenModule.cpp | 4 ++-- clang/lib/CIR/CodeGen/CIRGenModule.h | 14 +++++++------- clang/test/CIR/CodeGen/annotations-var.c | 8 ++++++++ 6 files changed, 26 insertions(+), 12 deletions(-) create mode 100644 clang/test/CIR/CodeGen/annotations-var.c diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index 20c998dbbd07..026cf10798e8 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -493,6 +493,7 @@ def AllocaOp : CIR_Op<"alloca", [ StrAttr:$name, UnitAttr:$init, ConfinedAttr, [IntMinValue<0>]>:$alignment, + OptionalAttr:$annotations, OptionalAttr:$ast ); @@ -530,6 +531,7 @@ def AllocaOp : CIR_Op<"alloca", [ `[` $name (`,` `init` $init^)? `]` + ($annotations^)? (`ast` $ast^)? attr-dict }]; diff --git a/clang/lib/CIR/CodeGen/CIRGenDecl.cpp b/clang/lib/CIR/CodeGen/CIRGenDecl.cpp index 3dbb656858e0..1f6692ef8163 100644 --- a/clang/lib/CIR/CodeGen/CIRGenDecl.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenDecl.cpp @@ -198,7 +198,7 @@ CIRGenFunction::buildAutoVarAlloca(const VarDecl &D, // Emit debug info for local var declaration. assert(!MissingFeatures::generateDebugInfo()); - if (D.hasAttr() && HaveInsertPoint()) + if (D.hasAttr()) buildVarAnnotations(&D, address.emitRawPointer()); // TODO(cir): in LLVM this calls @llvm.lifetime.end. diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp index e14565ed3082..2cec6203d3f0 100644 --- a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp @@ -1892,7 +1892,11 @@ mlir::Value CIRGenFunction::buildAlignmentAssumption( void CIRGenFunction::buildVarAnnotations(const VarDecl *decl, mlir::Value val) { assert(decl->hasAttr() && "no annotate attribute"); - for ([[maybe_unused]] const auto *I : decl->specific_attrs()) { - llvm_unreachable("NYI"); + llvm::SmallVector annotations; + for (const auto *annot : decl->specific_attrs()) { + annotations.push_back(CGM.buildAnnotateAttr(annot)); } + auto allocaOp = dyn_cast_or_null(val.getDefiningOp()); + assert(allocaOp && "expects available alloca"); + allocaOp.setAnnotationsAttr(builder.getArrayAttr(annotations)); } \ No newline at end of file diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index f8d39e40b6c9..21813577dbad 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -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 exprs = {attr->args_begin(), attr->args_size()}; if (exprs.empty()) { return mlir::ArrayAttr::get(builder.getContext(), {}); @@ -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); diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.h b/clang/lib/CIR/CodeGen/CIRGenModule.h index b652ec4f9ef7..1aee19d541e0 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.h +++ b/clang/lib/CIR/CodeGen/CIRGenModule.h @@ -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 MangledDeclNames; @@ -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. diff --git a/clang/test/CIR/CodeGen/annotations-var.c b/clang/test/CIR/CodeGen/annotations-var.c new file mode 100644 index 000000000000..cc617672dec0 --- /dev/null +++ b/clang/test/CIR/CodeGen/annotations-var.c @@ -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, ["localvar", init] [#cir.annotation, #cir.annotation] +} \ No newline at end of file