From da62b701e34051203e994e013eb372f0b243b287 Mon Sep 17 00:00:00 2001 From: rafasumi Date: Fri, 15 Aug 2025 10:04:30 -0300 Subject: [PATCH] [CIR] Attach hot attribute to CIR function --- clang/include/clang/CIR/Dialect/IR/CIRAttrs.td | 4 ++++ clang/lib/CIR/CodeGen/CIRGenModule.cpp | 6 ++++-- .../CIR/Lowering/DirectToLLVM/LowerToLLVMIR.cpp | 2 ++ clang/test/CIR/CodeGen/hot-attr.cpp | 16 ++++++++++++++++ 4 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 clang/test/CIR/CodeGen/hot-attr.cpp diff --git a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td index 73175e6dfd9c..211d4c142198 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td @@ -1201,6 +1201,10 @@ def CIR_ConvergentAttr : CIR_UnitAttr<"Convergent", "convergent"> { let storageType = [{ ConvergentAttr }]; } +def CIR_HotAttr : CIR_UnitAttr<"Hot", "hot"> { + let storageType = [{ HotAttr }]; +} + //===----------------------------------------------------------------------===// // UWTableAttr //===----------------------------------------------------------------------===// diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index d80c9a1f09e0..162d0e231f03 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -2983,8 +2983,10 @@ void CIRGenModule::setCIRFunctionAttributesForDefinition(const Decl *decl, if (decl->hasAttr()) { llvm_unreachable("NYI"); } - if (decl->hasAttr()) - llvm_unreachable("NYI"); + if (decl->hasAttr()) { + auto attr = cir::HotAttr::get(&getMLIRContext()); + attrs.set(attr.getMnemonic(), attr); + } if (decl->hasAttr()) assert(!MissingFeatures::minSize()); } diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMIR.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMIR.cpp index 55302cf3f0d9..7c77be265d50 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMIR.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMIR.cpp @@ -116,6 +116,8 @@ class CIRDialectLLVMIRTranslationInterface llvmFunc->addFnAttr(llvm::Attribute::NoUnwind); } else if (mlir::dyn_cast(attr.getValue())) { llvmFunc->addFnAttr(llvm::Attribute::Convergent); + } else if (mlir::dyn_cast(attr.getValue())) { + llvmFunc->addFnAttr(llvm::Attribute::Hot); } else if (mlir::dyn_cast(attr.getValue())) { const auto uniformAttrName = cir::OpenCLKernelUniformWorkGroupSizeAttr::getMnemonic(); diff --git a/clang/test/CIR/CodeGen/hot-attr.cpp b/clang/test/CIR/CodeGen/hot-attr.cpp new file mode 100644 index 000000000000..c7ae59d72ba7 --- /dev/null +++ b/clang/test/CIR/CodeGen/hot-attr.cpp @@ -0,0 +1,16 @@ +// RUN: %clang_cc1 -O2 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir +// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR +// RUN: %clang_cc1 -O2 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll +// RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM + +__attribute__((hot)) int s0(int a, int b) { + int x = a + b; + return x; +} + +// CIR: #[[ATTR0:.+]] = #cir !s32i extra(#[[ATTR0]]) + +// LLVM: define dso_local i32 @_Z2s0ii({{.*}} #[[#ATTR1:]] { +// LLVM: attributes #[[#ATTR1]] = {{.*}} hot