[clang] [CIR] Add ASTVarDeclInterface for AST attribute access (PR #179825)
Bruno Cardoso Lopes via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 12 16:52:44 PST 2026
https://github.com/bcardosolopes updated https://github.com/llvm/llvm-project/pull/179825
>From 912ed5a09323fc05a8487ae77d573c6ceedf8ee1 Mon Sep 17 00:00:00 2001
From: Bruno Cardoso Lopes <bruno.cardoso at gmail.com>
Date: Fri, 30 Jan 2026 14:36:53 -0800
Subject: [PATCH] [CIR] Add ASTVarDeclInterface for AST attribute access
Add the ASTVarDeclInterface which provides methods to access clang AST
VarDecl information from CIR attributes. This interface enables:
- mangleStaticGuardVariable: Mangle guard variable names using clang's
MangleContext
- isLocalVarDecl: Check if a variable is function-local
- getTLSKind: Get thread-local storage kind
- isInline: Check if the variable is inline
- getTemplateSpecializationKind: Get template specialization info
- getVarDecl: Direct access to the underlying VarDecl pointer
This infrastructure is needed for proper handling of static local
variables with guard variables in LoweringPrepare.
---
.../clang/CIR/Interfaces/ASTAttrInterfaces.h | 22 ++++++++
.../clang/CIR/Interfaces/ASTAttrInterfaces.td | 51 +++++++++++++++++++
.../clang/CIR/Interfaces/CMakeLists.txt | 9 ++++
.../lib/CIR/Interfaces/ASTAttrInterfaces.cpp | 21 ++++++++
clang/lib/CIR/Interfaces/CMakeLists.txt | 2 +
5 files changed, 105 insertions(+)
create mode 100644 clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h
create mode 100644 clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td
create mode 100644 clang/lib/CIR/Interfaces/ASTAttrInterfaces.cpp
diff --git a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h
new file mode 100644
index 0000000000000..12a6bfa8691cc
--- /dev/null
+++ b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h
@@ -0,0 +1,22 @@
+//===- ASTAttrInterfaces.h - CIR AST Interfaces -----------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef CLANG_CIR_INTERFACES_ASTATTRINTERFACES_H
+#define CLANG_CIR_INTERFACES_ASTATTRINTERFACES_H
+
+#include "mlir/IR/Attributes.h"
+#include "llvm/Support/raw_ostream.h"
+
+#include "clang/AST/Attr.h"
+#include "clang/AST/DeclTemplate.h"
+#include "clang/AST/Mangle.h"
+
+/// Include the generated interface declarations.
+#include "clang/CIR/Interfaces/ASTAttrInterfaces.h.inc"
+
+#endif // CLANG_CIR_INTERFACES_ASTATTRINTERFACES_H
diff --git a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td
new file mode 100644
index 0000000000000..c0d4da81cb979
--- /dev/null
+++ b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td
@@ -0,0 +1,51 @@
+//===- ASTAttrInterfaces.td - CIR AST Interface Definitions -----*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_CIR_INTERFACES_ASTATTRINTERFACES_TD
+#define MLIR_CIR_INTERFACES_ASTATTRINTERFACES_TD
+
+include "mlir/IR/OpBase.td"
+
+let cppNamespace = "::cir" in {
+ def ASTVarDeclInterface : AttrInterface<"ASTVarDeclInterface"> {
+ let methods = [
+ InterfaceMethod<"", "void", "mangleStaticGuardVariable",
+ (ins "llvm::raw_ostream&":$out), [{}],
+ /*defaultImplementation=*/[{
+ std::unique_ptr<clang::MangleContext> mangleCtx(
+ $_attr.getAst()->getASTContext().createMangleContext());
+ mangleCtx->mangleStaticGuardVariable($_attr.getAst(), out);
+ }]>,
+ InterfaceMethod<"", "bool", "isLocalVarDecl", (ins), [{}],
+ /*defaultImplementation=*/[{
+ return $_attr.getAst()->isLocalVarDecl();
+ }]>,
+ InterfaceMethod<"", "clang::VarDecl::TLSKind", "getTLSKind",
+ (ins), [{}],
+ /*defaultImplementation=*/[{
+ return $_attr.getAst()->getTLSKind();
+ }]>,
+ InterfaceMethod<"", "bool", "isInline", (ins), [{}],
+ /*defaultImplementation=*/[{
+ return $_attr.getAst()->isInline();
+ }]>,
+ InterfaceMethod<"", "clang::TemplateSpecializationKind",
+ "getTemplateSpecializationKind", (ins), [{}],
+ /*defaultImplementation=*/[{
+ return $_attr.getAst()->getTemplateSpecializationKind();
+ }]>,
+ InterfaceMethod<"Get the underlying VarDecl pointer.",
+ "const clang::VarDecl *", "getVarDecl", (ins), [{}],
+ /*defaultImplementation=*/[{
+ return $_attr.getAst();
+ }]>
+ ];
+ }
+} // namespace cir
+
+#endif // MLIR_CIR_INTERFACES_ASTATTRINTERFACES_TD
diff --git a/clang/include/clang/CIR/Interfaces/CMakeLists.txt b/clang/include/clang/CIR/Interfaces/CMakeLists.txt
index bc8d94ff9dc56..b7f8bd2f64cdb 100644
--- a/clang/include/clang/CIR/Interfaces/CMakeLists.txt
+++ b/clang/include/clang/CIR/Interfaces/CMakeLists.txt
@@ -3,6 +3,14 @@
# directory which is not the case for CIR (and also FIR, both have similar
# workarounds).
+function(add_clang_mlir_attr_interface interface)
+ set(LLVM_TARGET_DEFINITIONS ${interface}.td)
+ mlir_tablegen(${interface}.h.inc -gen-attr-interface-decls)
+ mlir_tablegen(${interface}.cpp.inc -gen-attr-interface-defs)
+ add_public_tablegen_target(MLIRCIR${interface}IncGen)
+ add_dependencies(mlir-generic-headers MLIRCIR${interface}IncGen)
+endfunction()
+
function(add_clang_mlir_op_interface interface)
set(LLVM_TARGET_DEFINITIONS ${interface}.td)
mlir_tablegen(${interface}.h.inc -gen-op-interface-decls)
@@ -19,6 +27,7 @@ function(add_clang_mlir_type_interface interface)
add_dependencies(mlir-generic-headers MLIR${interface}IncGen)
endfunction()
+add_clang_mlir_attr_interface(ASTAttrInterfaces)
add_clang_mlir_op_interface(CIROpInterfaces)
add_clang_mlir_op_interface(CIRLoopOpInterface)
add_clang_mlir_type_interface(CIRTypeInterfaces)
diff --git a/clang/lib/CIR/Interfaces/ASTAttrInterfaces.cpp b/clang/lib/CIR/Interfaces/ASTAttrInterfaces.cpp
new file mode 100644
index 0000000000000..cffcc94de695d
--- /dev/null
+++ b/clang/lib/CIR/Interfaces/ASTAttrInterfaces.cpp
@@ -0,0 +1,21 @@
+//====- ASTAttrInterfaces.cpp - Interface to AST Attributes ---------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Defines the interface to AST variable declaration attributes.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/CIR/Interfaces/ASTAttrInterfaces.h"
+#include "clang/AST/Decl.h"
+#include "clang/AST/Mangle.h"
+#include "clang/CIR/Dialect/IR/CIRAttrs.h"
+
+using namespace cir;
+
+/// Include the generated attribute interfaces.
+#include "clang/CIR/Interfaces/ASTAttrInterfaces.cpp.inc"
diff --git a/clang/lib/CIR/Interfaces/CMakeLists.txt b/clang/lib/CIR/Interfaces/CMakeLists.txt
index 13e4c2040f1c7..33eb169708a4f 100644
--- a/clang/lib/CIR/Interfaces/CMakeLists.txt
+++ b/clang/lib/CIR/Interfaces/CMakeLists.txt
@@ -1,4 +1,5 @@
add_clang_library(MLIRCIRInterfaces
+ ASTAttrInterfaces.cpp
CIROpInterfaces.cpp
CIRLoopOpInterface.cpp
CIRTypeInterfaces.cpp
@@ -7,6 +8,7 @@ add_clang_library(MLIRCIRInterfaces
${MLIR_MAIN_INCLUDE_DIR}/mlir/Interfaces
DEPENDS
+ MLIRCIRASTAttrInterfacesIncGen
MLIRCIREnumsGen
MLIRCIRTypeInterfacesIncGen
MLIRCIRLoopOpInterfaceIncGen
More information about the cfe-commits
mailing list