Files
llvm-project/clang/test/CIR/CodeGen/annotate-attribute.cpp
Bruno Cardoso Lopes 2948f9a784 [CIR] Add __attribute__((annotate(...))) support (#193329)
Implement end-to-end (Dialect, CIRGen, Lowering) support for
__attribute__((annotate(...))) on functions and global variables.

Dialect:
  - cir::AnnotationAttr (#cir.annotation<name = "...", args = [...]>).
- $annotations on cir.func and cir.global (ArrayAttr of AnnotationAttr).
FuncOp gets a custom parser/printer entry; for GlobalOp it's tacked onto
the assemblyFormat.

CIRGen:
- Annotations are attached as deferred entries in CIRGenModule and
flushed at end-of-TU so the most up-to-date ValueDecl wins (decl
annotations + def annotations both stick).
- emitAnnotationArgs uniques arg ArrayAttrs via FoldingSetNodeID so two
functions with identical args share one ArrayAttr (and one .args
constant in LLVM lowering).
- addGlobalAnnotations supports both GlobalOp and FuncOp paths,
including function-local statics (emitStaticVarDecl).

Lowering:
  - CXXABILowering treats AnnotationAttr as legal (no ABI conversion).
- Direct-to-LLVM lowering filters the annotations attribute off
cir.func, then collects (sym_name, AnnotationAttr, loc) tuples
pre-conversion and post-conversion emits @llvm.global.annotations plus
the supporting strings/args constants in section "llvm.metadata" —
bit-for-bit comparable to OGCG. String constants and per-arg structs are
deduplicated; identical args ArrayAttrs share one .args global (an
improvement over OGCG's per-site copies).
2026-04-24 12:04:40 -07:00

61 lines
2.6 KiB
C++

// RUN: %clang_cc1 -triple aarch64-linux-android29 -fclangir -emit-cir %s -o %t.cir
// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s
// RUN: %clang_cc1 -triple aarch64-linux-android29 -fclangir -emit-llvm %s -o %t-cir.ll
// RUN: FileCheck --check-prefix=LLVM --input-file=%t-cir.ll %s
// RUN: %clang_cc1 -triple aarch64-linux-android29 -emit-llvm %s -o %t.ll
// RUN: FileCheck --check-prefix=OGCG --input-file=%t.ll %s
// Verify that __attribute__((annotate(...))) on C++ entities works through
// the CXX ABI lowering pipeline. The CXXABILowering pass walks all attributes
// on functions/globals and asserts they are "legal" (i.e. don't need ABI
// conversion). cir::AnnotationAttr was added to that legal-list; this test
// covers that path by exercising annotated members and free functions in a
// translation unit that goes through C++ codegen (mangling, member functions,
// constructor emission).
// All globals are emitted before any function in CIR/LLVM/OGCG output;
// collect global-section checks up here so subsequent function CHECKs can
// rely on strict-order matching for the function section.
// CIR-DAG: cir.global external @{{.*ns.*g.*}} = #cir.int<5> : !s32i [#cir.annotation<"ns_global_ann">]
// LLVM-DAG: @{{.*ns.*g.*}} = global i32 5
// OGCG-DAG: @{{.*ns.*g.*}} = {{.*}}global i32 5
// OGCG-DAG: @llvm.global.annotations = appending global
// CIR-side LLVM lowering also emits @llvm.global.annotations.
// LLVM-DAG: @llvm.global.annotations = appending global
struct __attribute__((annotate("type_ann"))) Tagged {
int x;
__attribute__((annotate("method_ann")))
int get() { return x; }
__attribute__((annotate("static_method_ann")))
static int sget() { return 7; }
};
// CIR: cir.func {{.*}} @{{.*Tagged.*get.*}}({{.*}}) {{.*}}[#cir.annotation<"method_ann">]
// CIR: cir.func {{.*}} @{{.*Tagged.*sget.*}}() {{.*}}[#cir.annotation<"static_method_ann">]
// LLVM: define{{.*}} i32 @{{.*Tagged.*get.*}}
// LLVM: define{{.*}} i32 @{{.*Tagged.*sget.*}}
// OGCG-DAG: define{{.*}} i32 @{{.*Tagged.*get.*}}
// OGCG-DAG: define{{.*}} i32 @{{.*Tagged.*sget.*}}
__attribute__((annotate("free_fn_ann")))
int use_tagged(Tagged *t) {
return t->get() + Tagged::sget();
}
// CIR: cir.func {{.*}} @{{.*use_tagged.*}}({{.*}}) {{.*}}[#cir.annotation<"free_fn_ann">]
// LLVM: define{{.*}} i32 @{{.*use_tagged.*}}
// OGCG-DAG: define{{.*}} i32 @{{.*use_tagged.*}}
// Annotated namespace-scope global (C++ name mangling exercises a different
// path than C globals). The CIR-DAG / LLVM-DAG / OGCG-DAG checks for the
// emitted global live in the globals block at the top.
namespace ns {
__attribute__((annotate("ns_global_ann")))
int g = 5;
}