Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CIR] Make AST attributes accessible via interfaces. #250

Merged
merged 25 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e7cae09
[CIR] Introduce AST attribute interfaces.
xlauko Aug 28, 2023
6699584
[CIR][Interfaces] Setup hierarchy for Decl interfaces.
xlauko Aug 28, 2023
85d4d62
[CIR] Include AST interfaces into dialect.
xlauko Aug 28, 2023
e841f46
[CIR] Fix default implementation of AST interfaces.
xlauko Aug 28, 2023
f33288a
[CIR][IR] Introduce AST type attributes mimicing its interfaces.
xlauko Aug 28, 2023
f1ba4e8
[CIR][IR] Fix names and clean up interface.
xlauko Sep 1, 2023
fc057c9
[CIR] Fix recursive cmake dependencies of attr interfaces.
xlauko Sep 1, 2023
c948095
[CIR] Add AST interface methods to access record information.
xlauko Sep 1, 2023
0a046c5
[CIR][IR] Let StructType use AST arbitrary attribute.
xlauko Sep 1, 2023
7a9c0e7
[CIR][CIRGen] Use ASTCXXRecordDeclAttr in record builder.
xlauko Sep 1, 2023
7b14005
[CIR] Remove generated attribute printers and parsers.
xlauko Sep 1, 2023
563248e
[CIR][Lifetime] Use ASTDeclInterface instead of direct AST access.
xlauko Sep 1, 2023
6305a99
[CIR] Update expected AST attributes names in tests.
xlauko Sep 1, 2023
c84a092
[CIR] Replace VarDeclAttr with the corresponging interface.
xlauko Sep 1, 2023
7a3b7ab
[CIR] Replace FunctionDeclAttr with the corresponging interfaces.
xlauko Sep 1, 2023
eea1390
[CIR] Clean up decl interfaces.
xlauko Sep 2, 2023
41ee671
[CIR] Unify AST attr construction.
xlauko Sep 2, 2023
ac519c1
[CIR] Add TLS and Tag kind to AST Interface.
xlauko Sep 2, 2023
dcecbed
[CIR] Apply formatting to attr interface changes.
xlauko Sep 7, 2023
94c26b2
[CIR] Add interface method to query promise type existence.
xlauko Sep 7, 2023
82c8886
[CIR] Use ASTVarDeclInterface to constrain AST in VarOp and GlobalOp.
xlauko Sep 7, 2023
784333e
[CIR] Add missing tablegen dependencies.
xlauko Sep 8, 2023
81e9219
[CIR] Make AST Function attributes constrained.
xlauko Sep 8, 2023
019b556
[CIR] Refactor AST record interface and make it more type safe.
xlauko Sep 8, 2023
d022d25
[CIR][IR] Remove duplicit optional of struct ast parameter.
xlauko Sep 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -16,8 +16,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/LoopLikeInterface.td"
Expand Down Expand Up @@ -293,7 +295,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 @@ -1277,7 +1279,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 @@ -1590,7 +1592,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