Skip to content

Commit

Permalink
[CIR] Make AST attributes accessible via interfaces. (#250)
Browse files Browse the repository at this point in the history
- 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`.
  • Loading branch information
xlauko authored and lanza committed Mar 22, 2024
1 parent e2e785e commit 0d02dc7
Show file tree
Hide file tree
Showing 41 changed files with 508 additions and 195 deletions.
7 changes: 7 additions & 0 deletions clang/include/clang/CIR/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
set(MLIR_MAIN_SRC_DIR ${LLVM_MAIN_SRC_DIR}/../mlir/include ) # --src-root
set(MLIR_INCLUDE_DIR ${LLVM_MAIN_SRC_DIR}/../mlir/include ) # --includedir
set(MLIR_TABLEGEN_OUTPUT_DIR ${CMAKE_BINARY_DIR}/tools/mlir/include)
include_directories(SYSTEM ${MLIR_INCLUDE_DIR})
include_directories(SYSTEM ${MLIR_TABLEGEN_OUTPUT_DIR})

add_subdirectory(Dialect)
add_subdirectory(Interfaces)
6 changes: 0 additions & 6 deletions clang/include/clang/CIR/Dialect/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
set(MLIR_MAIN_SRC_DIR ${LLVM_MAIN_SRC_DIR}/../mlir/include ) # --src-root
set(MLIR_INCLUDE_DIR ${LLVM_MAIN_SRC_DIR}/../mlir/include ) # --includedir
set(MLIR_TABLEGEN_OUTPUT_DIR ${CMAKE_BINARY_DIR}/tools/mlir/include)
include_directories(SYSTEM ${MLIR_INCLUDE_DIR})
include_directories(SYSTEM ${MLIR_TABLEGEN_OUTPUT_DIR})

add_custom_target(clang-cir-doc)

# This replicates part of the add_mlir_doc cmake function from MLIR that cannot
Expand Down
4 changes: 4 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIRAttrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
#include "mlir/IR/Attributes.h"
#include "mlir/IR/BuiltinAttributeInterfaces.h"

#include "llvm/ADT/SmallVector.h"

#include "clang/CIR/Dialect/IR/CIROpsEnums.h"

#include "clang/CIR/Interfaces/ASTAttrInterfaces.h"

//===----------------------------------------------------------------------===//
// CIR Dialect Attrs
//===----------------------------------------------------------------------===//
Expand Down
52 changes: 49 additions & 3 deletions clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@

include "mlir/IR/BuiltinAttributeInterfaces.td"
include "mlir/IR/EnumAttr.td"

include "clang/CIR/Dialect/IR/CIRDialect.td"

include "clang/CIR/Interfaces/ASTAttrInterfaces.td"

//===----------------------------------------------------------------------===//
// CIR Attrs
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -394,12 +397,55 @@ class ASTDecl<string name, string prefix, list<Trait> traits = []>

// Enable verifier.
let genVerifyDecl = 1;

let extraClassDefinition = [{
::mlir::Attribute $cppClass::parse(::mlir::AsmParser &parser,
::mlir::Type type) {
// We cannot really parse anything AST related at this point
// since we have no serialization/JSON story.
return $cppClass::get(parser.getContext(), nullptr);
}

void $cppClass::print(::mlir::AsmPrinter &printer) const {
// Nothing to print besides the mnemonics.
}

LogicalResult $cppClass::verify(
::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError,
}] # clang_name # [{ decl) {
return success();
}
}];
}

def ASTFunctionDeclAttr : ASTDecl<"FunctionDecl", "fndecl">;
def ASTVarDeclAttr : ASTDecl<"VarDecl", "vardecl">;
def ASTRecordDeclAttr : ASTDecl<"RecordDecl", "recdecl">;
def ASTDeclAttr : ASTDecl<"Decl", "decl", [ASTDeclInterface]>;

def ASTFunctionDeclAttr : ASTDecl<"FunctionDecl", "function.decl",
[ASTFunctionDeclInterface]>;

def ASTCXXMethodDeclAttr : ASTDecl<"CXXMethodDecl", "cxxmethod.decl",
[ASTCXXMethodDeclInterface]>;

def ASTCXXConstructorDeclAttr : ASTDecl<"CXXConstructorDecl",
"cxxconstructor.decl", [ASTCXXConstructorDeclInterface]>;

def ASTCXXConversionDeclAttr : ASTDecl<"CXXConversionDecl",
"cxxconversion.decl", [ASTCXXConversionDeclInterface]>;

def ASTCXXDestructorDeclAttr : ASTDecl<"CXXDestructorDecl",
"cxxdestructor.decl", [ASTCXXDestructorDeclInterface]>;

def ASTVarDeclAttr : ASTDecl<"VarDecl", "var.decl",
[ASTVarDeclInterface]>;

def ASTTypeDeclAttr: ASTDecl<"TypeDecl", "type.decl",
[ASTTypeDeclInterface]>;

def ASTTagDeclAttr : ASTDecl<"TagDecl", "tag.decl",
[ASTTagDeclInterface]>;

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

//===----------------------------------------------------------------------===//
// ExtraFuncAttr
Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIRDialect.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include "clang/CIR/Dialect/IR/CIROpsStructs.h.inc"
#include "clang/CIR/Dialect/IR/CIRTypes.h"

#include "clang/CIR/Interfaces/ASTAttrInterfaces.h"

namespace mlir {
namespace OpTrait {

Expand Down
8 changes: 5 additions & 3 deletions clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ include "clang/CIR/Dialect/IR/CIRDialect.td"
include "clang/CIR/Dialect/IR/CIRTypes.td"
include "clang/CIR/Dialect/IR/CIRAttrs.td"

include "clang/CIR/Interfaces/ASTAttrInterfaces.td"

include "mlir/Interfaces/CallInterfaces.td"
include "mlir/Interfaces/ControlFlowInterfaces.td"
include "mlir/Interfaces/FunctionInterfaces.td"
Expand Down Expand Up @@ -300,7 +302,7 @@ def AllocaOp : CIR_Op<"alloca", [
StrAttr:$name,
UnitAttr:$init,
ConfinedAttr<OptionalAttr<I64Attr>, [IntMinValue<0>]>:$alignment,
OptionalAttr<ASTVarDeclAttr>:$ast
OptionalAttr<ASTVarDeclInterface>:$ast
);

let results = (outs Res<CIR_PointerType, "",
Expand Down Expand Up @@ -1283,7 +1285,7 @@ def GlobalOp : CIR_Op<"global", [Symbol, DeclareOpInterfaceMethods<RegionBranchO
OptionalAttr<AnyAttr>:$initial_value,
UnitAttr:$constant,
OptionalAttr<I64Attr>:$alignment,
OptionalAttr<ASTVarDeclAttr>:$ast
OptionalAttr<ASTVarDeclInterface>:$ast
);
let regions = (region AnyRegion:$ctorRegion, AnyRegion:$dtorRegion);
let assemblyFormat = [{
Expand Down Expand Up @@ -1596,7 +1598,7 @@ def FuncOp : CIR_Op<"func", [
OptionalAttr<DictArrayAttr>:$arg_attrs,
OptionalAttr<DictArrayAttr>:$res_attrs,
OptionalAttr<FlatSymbolRefAttr>:$aliasee,
OptionalAttr<ASTFunctionDeclAttr>:$ast);
OptionalAttr<AnyASTFunctionDeclAttr>:$ast);
let regions = (region AnyRegion:$body);
let skipDefaultBuilders = 1;

Expand Down
8 changes: 2 additions & 6 deletions clang/include/clang/CIR/Dialect/IR/CIRTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@
#include "mlir/IR/Types.h"
#include "mlir/Interfaces/DataLayoutInterfaces.h"

#include "clang/CIR/Interfaces/ASTAttrInterfaces.h"

//===----------------------------------------------------------------------===//
// CIR Dialect Types
//===----------------------------------------------------------------------===//

namespace mlir {
namespace cir {
class ASTRecordDeclAttr;
} // namespace cir
} // namespace mlir

#define GET_TYPEDEF_CLASSES
#include "clang/CIR/Dialect/IR/CIROpsTypes.h.inc"

Expand Down
3 changes: 2 additions & 1 deletion clang/include/clang/CIR/Dialect/IR/CIRTypes.td
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define MLIR_CIR_DIALECT_CIR_TYPES

include "clang/CIR/Dialect/IR/CIRDialect.td"
include "clang/CIR/Interfaces/ASTAttrInterfaces.td"
include "mlir/Interfaces/DataLayoutInterfaces.td"
include "mlir/IR/AttrTypeBase.td"

Expand Down Expand Up @@ -111,7 +112,7 @@ def CIR_StructType : CIR_Type<"Struct", "struct",
"bool":$body,
"bool":$packed,
"mlir::cir::StructType::RecordKind":$kind,
"std::optional<::mlir::cir::ASTRecordDeclAttr>":$ast
"std::optional<ASTRecordDeclInterface>":$ast
);

let hasCustomAssemblyFormat = 1;
Expand Down
45 changes: 45 additions & 0 deletions clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//===- 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 MLIR_INTERFACES_CIR_AST_ATTR_INTERFACES_H_
#define MLIR_INTERFACES_CIR_AST_ATTR_INTERFACES_H_

#include "mlir/IR/Attributes.h"

#include "clang/AST/Attr.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/Mangle.h"

namespace mlir {
namespace cir {

mlir::Attribute makeFuncDeclAttr(const clang::Decl *decl,
mlir::MLIRContext *ctx);

} // namespace cir
} // namespace mlir

/// Include the generated interface declarations.
#include "clang/CIR/Interfaces/ASTAttrInterfaces.h.inc"

namespace mlir {
namespace cir {

template <typename T> bool hasAttr(ASTDeclInterface decl) {
if constexpr (std::is_same_v<T, clang::OwnerAttr>)
return decl.hasOwnerAttr();
if constexpr (std::is_same_v<T, clang::PointerAttr>)
return decl.hasPointerAttr();
if constexpr (std::is_same_v<T, clang::InitPriorityAttr>)
return decl.hasInitPriorityAttr();
}

} // namespace cir
} // namespace mlir

#endif // MLIR_INTERFACES_CIR_AST_ATAR_INTERFACES_H_
Loading

0 comments on commit 0d02dc7

Please sign in to comment.