Skip to content

Commit a4d12fc

Browse files
xlaukolanza
authored andcommitted
[CIR] Make AST attributes accessible via interfaces. (#250)
- Introduces `CIR/Interfaces/ASTAttrInterfaces` which model API of clang AST nodes, but allows to plugin custom attribute, making `CIR` dialect AST independent. - Extends hierarchy of `DeclAttr`s to model `Decl` attributes more faithfully. - Notably all `CIRASTAttr`s are now created uniformly using `makeAstDeclAttr` which builds corresponding Attribute based on `clang::Decl`.
1 parent 8b4fe01 commit a4d12fc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+509
-196
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1+
set(MLIR_MAIN_SRC_DIR ${LLVM_MAIN_SRC_DIR}/../mlir/include ) # --src-root
2+
set(MLIR_INCLUDE_DIR ${LLVM_MAIN_SRC_DIR}/../mlir/include ) # --includedir
3+
set(MLIR_TABLEGEN_OUTPUT_DIR ${CMAKE_BINARY_DIR}/tools/mlir/include)
4+
include_directories(SYSTEM ${MLIR_INCLUDE_DIR})
5+
include_directories(SYSTEM ${MLIR_TABLEGEN_OUTPUT_DIR})
6+
17
add_subdirectory(Dialect)
8+
add_subdirectory(Interfaces)

clang/include/clang/CIR/Dialect/CMakeLists.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
set(MLIR_MAIN_SRC_DIR ${LLVM_MAIN_SRC_DIR}/../mlir/include ) # --src-root
2-
set(MLIR_INCLUDE_DIR ${LLVM_MAIN_SRC_DIR}/../mlir/include ) # --includedir
3-
set(MLIR_TABLEGEN_OUTPUT_DIR ${CMAKE_BINARY_DIR}/tools/mlir/include)
4-
include_directories(SYSTEM ${MLIR_INCLUDE_DIR})
5-
include_directories(SYSTEM ${MLIR_TABLEGEN_OUTPUT_DIR})
6-
71
add_custom_target(clang-cir-doc)
82

93
# This replicates part of the add_mlir_doc cmake function from MLIR that cannot

clang/include/clang/CIR/Dialect/IR/CIRAttrs.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@
1616
#include "mlir/IR/Attributes.h"
1717
#include "mlir/IR/BuiltinAttributeInterfaces.h"
1818

19+
#include "llvm/ADT/SmallVector.h"
20+
1921
#include "clang/CIR/Dialect/IR/CIROpsEnums.h"
2022

23+
#include "clang/CIR/Interfaces/ASTAttrInterfaces.h"
24+
2125
//===----------------------------------------------------------------------===//
2226
// CIR Dialect Attrs
2327
//===----------------------------------------------------------------------===//

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

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515

1616
include "mlir/IR/BuiltinAttributeInterfaces.td"
1717
include "mlir/IR/EnumAttr.td"
18+
1819
include "clang/CIR/Dialect/IR/CIRDialect.td"
1920

21+
include "clang/CIR/Interfaces/ASTAttrInterfaces.td"
22+
2023
//===----------------------------------------------------------------------===//
2124
// CIR Attrs
2225
//===----------------------------------------------------------------------===//
@@ -394,12 +397,55 @@ class ASTDecl<string name, string prefix, list<Trait> traits = []>
394397

395398
// Enable verifier.
396399
let genVerifyDecl = 1;
400+
401+
let extraClassDefinition = [{
402+
::mlir::Attribute $cppClass::parse(::mlir::AsmParser &parser,
403+
::mlir::Type type) {
404+
// We cannot really parse anything AST related at this point
405+
// since we have no serialization/JSON story.
406+
return $cppClass::get(parser.getContext(), nullptr);
407+
}
408+
409+
void $cppClass::print(::mlir::AsmPrinter &printer) const {
410+
// Nothing to print besides the mnemonics.
411+
}
412+
413+
LogicalResult $cppClass::verify(
414+
::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError,
415+
}] # clang_name # [{ decl) {
416+
return success();
417+
}
418+
}];
397419
}
398420

399-
def ASTFunctionDeclAttr : ASTDecl<"FunctionDecl", "fndecl">;
400-
def ASTVarDeclAttr : ASTDecl<"VarDecl", "vardecl">;
401-
def ASTRecordDeclAttr : ASTDecl<"RecordDecl", "recdecl">;
421+
def ASTDeclAttr : ASTDecl<"Decl", "decl", [ASTDeclInterface]>;
422+
423+
def ASTFunctionDeclAttr : ASTDecl<"FunctionDecl", "function.decl",
424+
[ASTFunctionDeclInterface]>;
425+
426+
def ASTCXXMethodDeclAttr : ASTDecl<"CXXMethodDecl", "cxxmethod.decl",
427+
[ASTCXXMethodDeclInterface]>;
428+
429+
def ASTCXXConstructorDeclAttr : ASTDecl<"CXXConstructorDecl",
430+
"cxxconstructor.decl", [ASTCXXConstructorDeclInterface]>;
431+
432+
def ASTCXXConversionDeclAttr : ASTDecl<"CXXConversionDecl",
433+
"cxxconversion.decl", [ASTCXXConversionDeclInterface]>;
434+
435+
def ASTCXXDestructorDeclAttr : ASTDecl<"CXXDestructorDecl",
436+
"cxxdestructor.decl", [ASTCXXDestructorDeclInterface]>;
437+
438+
def ASTVarDeclAttr : ASTDecl<"VarDecl", "var.decl",
439+
[ASTVarDeclInterface]>;
440+
441+
def ASTTypeDeclAttr: ASTDecl<"TypeDecl", "type.decl",
442+
[ASTTypeDeclInterface]>;
443+
444+
def ASTTagDeclAttr : ASTDecl<"TagDecl", "tag.decl",
445+
[ASTTagDeclInterface]>;
402446

447+
def ASTRecordDeclAttr : ASTDecl<"RecordDecl", "record.decl",
448+
[ASTRecordDeclInterface]>;
403449

404450
//===----------------------------------------------------------------------===//
405451
// ExtraFuncAttr

clang/include/clang/CIR/Dialect/IR/CIRDialect.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#include "clang/CIR/Dialect/IR/CIROpsStructs.h.inc"
3232
#include "clang/CIR/Dialect/IR/CIRTypes.h"
3333

34+
#include "clang/CIR/Interfaces/ASTAttrInterfaces.h"
35+
3436
namespace mlir {
3537
namespace OpTrait {
3638

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ include "clang/CIR/Dialect/IR/CIRDialect.td"
1818
include "clang/CIR/Dialect/IR/CIRTypes.td"
1919
include "clang/CIR/Dialect/IR/CIRAttrs.td"
2020

21+
include "clang/CIR/Interfaces/ASTAttrInterfaces.td"
22+
2123
include "mlir/Interfaces/CallInterfaces.td"
2224
include "mlir/Interfaces/ControlFlowInterfaces.td"
2325
include "mlir/Interfaces/LoopLikeInterface.td"
@@ -293,7 +295,7 @@ def AllocaOp : CIR_Op<"alloca", [
293295
StrAttr:$name,
294296
UnitAttr:$init,
295297
ConfinedAttr<OptionalAttr<I64Attr>, [IntMinValue<0>]>:$alignment,
296-
OptionalAttr<ASTVarDeclAttr>:$ast
298+
OptionalAttr<ASTVarDeclInterface>:$ast
297299
);
298300

299301
let results = (outs Res<CIR_PointerType, "",
@@ -1277,7 +1279,7 @@ def GlobalOp : CIR_Op<"global", [Symbol, DeclareOpInterfaceMethods<RegionBranchO
12771279
OptionalAttr<AnyAttr>:$initial_value,
12781280
UnitAttr:$constant,
12791281
OptionalAttr<I64Attr>:$alignment,
1280-
OptionalAttr<ASTVarDeclAttr>:$ast
1282+
OptionalAttr<ASTVarDeclInterface>:$ast
12811283
);
12821284
let regions = (region AnyRegion:$ctorRegion, AnyRegion:$dtorRegion);
12831285
let assemblyFormat = [{
@@ -1590,7 +1592,7 @@ def FuncOp : CIR_Op<"func", [
15901592
OptionalAttr<DictArrayAttr>:$arg_attrs,
15911593
OptionalAttr<DictArrayAttr>:$res_attrs,
15921594
OptionalAttr<FlatSymbolRefAttr>:$aliasee,
1593-
OptionalAttr<ASTFunctionDeclAttr>:$ast);
1595+
OptionalAttr<AnyASTFunctionDeclAttr>:$ast);
15941596
let regions = (region AnyRegion:$body);
15951597
let skipDefaultBuilders = 1;
15961598

clang/include/clang/CIR/Dialect/IR/CIRTypes.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,12 @@
1717
#include "mlir/IR/Types.h"
1818
#include "mlir/Interfaces/DataLayoutInterfaces.h"
1919

20+
#include "clang/CIR/Interfaces/ASTAttrInterfaces.h"
21+
2022
//===----------------------------------------------------------------------===//
2123
// CIR Dialect Types
2224
//===----------------------------------------------------------------------===//
2325

24-
namespace mlir {
25-
namespace cir {
26-
class ASTRecordDeclAttr;
27-
} // namespace cir
28-
} // namespace mlir
29-
3026
#define GET_TYPEDEF_CLASSES
3127
#include "clang/CIR/Dialect/IR/CIROpsTypes.h.inc"
3228

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define MLIR_CIR_DIALECT_CIR_TYPES
1515

1616
include "clang/CIR/Dialect/IR/CIRDialect.td"
17+
include "clang/CIR/Interfaces/ASTAttrInterfaces.td"
1718
include "mlir/Interfaces/DataLayoutInterfaces.td"
1819
include "mlir/IR/AttrTypeBase.td"
1920

@@ -111,7 +112,7 @@ def CIR_StructType : CIR_Type<"Struct", "struct",
111112
"bool":$body,
112113
"bool":$packed,
113114
"mlir::cir::StructType::RecordKind":$kind,
114-
"std::optional<::mlir::cir::ASTRecordDeclAttr>":$ast
115+
"std::optional<ASTRecordDeclInterface>":$ast
115116
);
116117

117118
let hasCustomAssemblyFormat = 1;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//===- ASTAttrInterfaces.h - CIR AST Interfaces -----------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef MLIR_INTERFACES_CIR_AST_ATTR_INTERFACES_H_
10+
#define MLIR_INTERFACES_CIR_AST_ATTR_INTERFACES_H_
11+
12+
#include "mlir/IR/Attributes.h"
13+
14+
#include "clang/AST/Attr.h"
15+
#include "clang/AST/DeclTemplate.h"
16+
#include "clang/AST/Mangle.h"
17+
18+
namespace mlir {
19+
namespace cir {
20+
21+
mlir::Attribute makeFuncDeclAttr(const clang::Decl *decl,
22+
mlir::MLIRContext *ctx);
23+
24+
} // namespace cir
25+
} // namespace mlir
26+
27+
/// Include the generated interface declarations.
28+
#include "clang/CIR/Interfaces/ASTAttrInterfaces.h.inc"
29+
30+
namespace mlir {
31+
namespace cir {
32+
33+
template <typename T> bool hasAttr(ASTDeclInterface decl) {
34+
if constexpr (std::is_same_v<T, clang::OwnerAttr>)
35+
return decl.hasOwnerAttr();
36+
if constexpr (std::is_same_v<T, clang::PointerAttr>)
37+
return decl.hasPointerAttr();
38+
if constexpr (std::is_same_v<T, clang::InitPriorityAttr>)
39+
return decl.hasInitPriorityAttr();
40+
}
41+
42+
} // namespace cir
43+
} // namespace mlir
44+
45+
#endif // MLIR_INTERFACES_CIR_AST_ATAR_INTERFACES_H_

0 commit comments

Comments
 (0)