From e7cae099b080a2be70ed469d1226050509513f5f Mon Sep 17 00:00:00 2001 From: Henrich Lauko Date: Mon, 28 Aug 2023 14:08:53 +0200 Subject: [PATCH 01/25] [CIR] Introduce AST attribute interfaces. --- clang/include/clang/CIR/CMakeLists.txt | 7 ++ .../include/clang/CIR/Dialect/CMakeLists.txt | 6 -- .../include/clang/CIR/Dialect/IR/CIRDialect.h | 2 + .../clang/CIR/Interfaces/ASTAttrInterfaces.h | 19 +++++ .../clang/CIR/Interfaces/ASTAttrInterfaces.td | 83 +++++++++++++++++++ .../clang/CIR/Interfaces/CMakeLists.txt | 15 ++++ clang/lib/CIR/CMakeLists.txt | 1 + clang/lib/CIR/CodeGen/CMakeLists.txt | 2 + clang/lib/CIR/Dialect/IR/CMakeLists.txt | 1 + clang/lib/CIR/FrontendAction/CMakeLists.txt | 1 + .../lib/CIR/Interfaces/ASTAttrInterfaces.cpp | 11 +++ clang/lib/CIR/Interfaces/CMakeLists.txt | 17 ++++ .../CIR/Lowering/DirectToLLVM/CMakeLists.txt | 1 + .../CIR/Lowering/ThroughMLIR/CMakeLists.txt | 1 + clang/tools/cir-opt/CMakeLists.txt | 1 + 15 files changed, 162 insertions(+), 6 deletions(-) create mode 100644 clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h create mode 100644 clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td create mode 100644 clang/include/clang/CIR/Interfaces/CMakeLists.txt create mode 100644 clang/lib/CIR/Interfaces/ASTAttrInterfaces.cpp create mode 100644 clang/lib/CIR/Interfaces/CMakeLists.txt diff --git a/clang/include/clang/CIR/CMakeLists.txt b/clang/include/clang/CIR/CMakeLists.txt index 0ca0f41c5af4..2028af5232c2 100644 --- a/clang/include/clang/CIR/CMakeLists.txt +++ b/clang/include/clang/CIR/CMakeLists.txt @@ -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) diff --git a/clang/include/clang/CIR/Dialect/CMakeLists.txt b/clang/include/clang/CIR/Dialect/CMakeLists.txt index 383bf5231f57..cd837615e82f 100644 --- a/clang/include/clang/CIR/Dialect/CMakeLists.txt +++ b/clang/include/clang/CIR/Dialect/CMakeLists.txt @@ -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 diff --git a/clang/include/clang/CIR/Dialect/IR/CIRDialect.h b/clang/include/clang/CIR/Dialect/IR/CIRDialect.h index a5bbcf2ca91c..7756092404af 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRDialect.h +++ b/clang/include/clang/CIR/Dialect/IR/CIRDialect.h @@ -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 { diff --git a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h new file mode 100644 index 000000000000..10f07278fd7a --- /dev/null +++ b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h @@ -0,0 +1,19 @@ +//===- 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_ATR_INTERFACES_H_ +#define MLIR_INTERFACES_CIR_AST_ATR_INTERFACES_H_ + +#include "mlir/IR/Attributes.h" + +#include "clang/AST/Attr.h" + +/// Include the generated interface declarations. +#include "clang/CIR/Interfaces/ASTAttrInterfaces.h.inc" + +#endif // MLIR_INTERFACES_CIR_AST_ATR_INTERFACES_H_ diff --git a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td new file mode 100644 index 000000000000..33bef5615d0a --- /dev/null +++ b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td @@ -0,0 +1,83 @@ +//===- 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_AST_ATTR_INTERFACES +#define MLIR_CIR_INTERFACES_AST_ATTR_INTERFACES + +include "mlir/IR/OpBase.td" + +let cppNamespace = "::mlir::cir" in { + + def ASTDeclInterface : AttrInterface< "ASTDeclInterface" > { + let methods = [ + // hasAttr, cannot be templated :( + InterfaceMethod< "", "bool", "hasOwnerAttr", (ins), + /* methodBody = */ [{}], + /* defaultImplementation= */[{ + return $_attr.getDecl()->template hasAttr< clang::OwnerAttr >(); + }] + >, + InterfaceMethod< "", "bool", "hasPointerAttr", (ins), + /* methodBody = */ [{}], + /* defaultImplementation= */[{ + return $_attr.getDecl()->template hasAttr< clang::PointerAttr >(); + }] + > + ]; + } + + // def ASTNamedDeclInterface : AttrInterface< "ASTNamedDeclInterface", [ASTDeclInterface] > {} + + // def ASTTypeDeclInterface : AttrInterface< "ASTTypeDeclInterface", [ASTNamedDeclInterface] > {} + + // def ASTTagDeclInterface : AttrInterface< "ASTTagDeclInterface", [ASTTypeDeclInterface] > {} + + // def ASTRecordDeclInterface : AttrInterface< "ASTRecordDeclInterface", [ASTTagDeclInterface] > {} + + def ASTCXXRecorDeclInterface : AttrInterface< "ASTCXXRecorDeclInterface", [ASTDeclInterface] > { + let methods = [ + InterfaceMethod< "", "bool", "isLambda", (ins), [{}], + /*defaultImplementation=*/ [{ + return $_attr.getDecl()->isLambda(); + }] + > + ]; + } + + def ASTVarDeclInterface : AttrInterface< "ASTVarDeclInterface", [ASTDeclInterface] > {} + + def ASTFunctionDeclInterface : AttrInterface< "ASTFunctionDeclInterface", [ASTDeclInterface] > { + let methods = [ + InterfaceMethod< "", "bool", "isOverloadedOperator", (ins), [{}], + /*defaultImplementation=*/ [{ + return $_attr.getDecl()->isOverloadedOperator(); + }] + >, + // TODO move to ASTMethodDeclInterface + InterfaceMethod< "", "bool", "isCopyAssignmentOperator", (ins), [{}], + /*defaultImplementation=*/ [{ + if (auto decl = dyn_cast< clang::CXXMethodDecl >($_attr.getDecl())) + return decl->isCopyAssignmentOperator(); + return false; + }] + >, + // TODO move to ASTMethodDeclInterface + InterfaceMethod< "", "bool", "isMoveAssignmentOperator", (ins), [{}], + /*defaultImplementation=*/ [{ + if (auto decl = dyn_cast< clang::CXXMethodDecl >($_attr.getDecl())) + return decl->isMoveAssignmentOperator(); + return false; + }] + > + ]; + } + + def ASTCXXMethodDeclInterface : AttrInterface< "ASTCXXMethodDeclInterface", [ASTDeclInterface] > {} +} + +#endif // MLIR_CIR_INTERFACES_AST_ATTR_INTERFACES diff --git a/clang/include/clang/CIR/Interfaces/CMakeLists.txt b/clang/include/clang/CIR/Interfaces/CMakeLists.txt new file mode 100644 index 000000000000..6925b69a2c97 --- /dev/null +++ b/clang/include/clang/CIR/Interfaces/CMakeLists.txt @@ -0,0 +1,15 @@ +# This replicates part of the add_mlir_interface cmake function from MLIR that +# cannot be used here. This happens because it expects to be run inside MLIR +# directory which is not the case for CIR (and also FIR, both have similar +# workarounds). + +# Declare a dialect in the include directory +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() + +add_clang_mlir_attr_interface(ASTAttrInterfaces) diff --git a/clang/lib/CIR/CMakeLists.txt b/clang/lib/CIR/CMakeLists.txt index 41e07837d21d..093420b4fee3 100644 --- a/clang/lib/CIR/CMakeLists.txt +++ b/clang/lib/CIR/CMakeLists.txt @@ -5,3 +5,4 @@ add_subdirectory(Dialect) add_subdirectory(CodeGen) add_subdirectory(FrontendAction) add_subdirectory(Lowering) +add_subdirectory(Interfaces) diff --git a/clang/lib/CIR/CodeGen/CMakeLists.txt b/clang/lib/CIR/CodeGen/CMakeLists.txt index 7c0474aee006..a379ed464316 100644 --- a/clang/lib/CIR/CodeGen/CMakeLists.txt +++ b/clang/lib/CIR/CodeGen/CMakeLists.txt @@ -38,6 +38,7 @@ add_clang_library(clangCIR DEPENDS MLIRCIR MLIRCIROpsIncGen + MLIRCIRASTAttrInterfacesIncGen ${dialect_libs} LINK_LIBS @@ -47,6 +48,7 @@ add_clang_library(clangCIR ${dialect_libs} MLIRCIR MLIRCIRTransforms + MLIRCIRASTAttrInterfaces MLIRAffineToStandard MLIRAnalysis MLIRDLTIDialect diff --git a/clang/lib/CIR/Dialect/IR/CMakeLists.txt b/clang/lib/CIR/Dialect/IR/CMakeLists.txt index edf18e89ca80..a035b1e51c29 100644 --- a/clang/lib/CIR/Dialect/IR/CMakeLists.txt +++ b/clang/lib/CIR/Dialect/IR/CMakeLists.txt @@ -9,6 +9,7 @@ add_clang_library(MLIRCIR MLIRCIROpsIncGen MLIRCIREnumsGen MLIRSymbolInterfacesIncGen + MLIRCIRASTAttrInterfacesIncGen LINK_LIBS PUBLIC MLIRIR diff --git a/clang/lib/CIR/FrontendAction/CMakeLists.txt b/clang/lib/CIR/FrontendAction/CMakeLists.txt index c223383d24cf..b2655831fcc6 100644 --- a/clang/lib/CIR/FrontendAction/CMakeLists.txt +++ b/clang/lib/CIR/FrontendAction/CMakeLists.txt @@ -10,6 +10,7 @@ add_clang_library(clangCIRFrontendAction DEPENDS MLIRCIROpsIncGen + MLIRCIRASTAttrInterfacesIncGen LINK_LIBS clangAST diff --git a/clang/lib/CIR/Interfaces/ASTAttrInterfaces.cpp b/clang/lib/CIR/Interfaces/ASTAttrInterfaces.cpp new file mode 100644 index 000000000000..8dd24b7384b6 --- /dev/null +++ b/clang/lib/CIR/Interfaces/ASTAttrInterfaces.cpp @@ -0,0 +1,11 @@ +//====- 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 +// +//===----------------------------------------------------------------------===// +#include "clang/CIR/Interfaces/ASTAttrInterfaces.h" + +/// Include the generated type qualifiers interfaces. +#include "clang/CIR/Interfaces/ASTAttrInterfaces.cpp.inc" diff --git a/clang/lib/CIR/Interfaces/CMakeLists.txt b/clang/lib/CIR/Interfaces/CMakeLists.txt new file mode 100644 index 000000000000..48acb88587b6 --- /dev/null +++ b/clang/lib/CIR/Interfaces/CMakeLists.txt @@ -0,0 +1,17 @@ +add_clang_library(MLIRCIRASTAttrInterfaces + ASTAttrInterfaces.cpp + + ADDITIONAL_HEADER_DIRS + ${MLIR_MAIN_INCLUDE_DIR}/mlir/Interfaces + + DEPENDS + MLIRCIRASTAttrInterfacesIncGen + + LINK_LIBS + clangCIR + ${dialect_libs} + MLIRCIR + MLIRAnalysis + MLIRIR + MLIRSupport + ) diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt b/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt index c7f713e85da0..5ffc4cefc6cd 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt +++ b/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt @@ -12,6 +12,7 @@ add_clang_library(clangCIRLoweringDirectToLLVM DEPENDS MLIRCIREnumsGen MLIRCIROpsIncGen + MLIRCIRASTAttrInterfacesIncGen LINK_LIBS clangAST diff --git a/clang/lib/CIR/Lowering/ThroughMLIR/CMakeLists.txt b/clang/lib/CIR/Lowering/ThroughMLIR/CMakeLists.txt index 762e10eb5db3..b0942512e1b8 100644 --- a/clang/lib/CIR/Lowering/ThroughMLIR/CMakeLists.txt +++ b/clang/lib/CIR/Lowering/ThroughMLIR/CMakeLists.txt @@ -12,6 +12,7 @@ add_clang_library(clangCIRLoweringThroughMLIR DEPENDS MLIRCIROpsIncGen MLIRCIREnumsGen + MLIRCIRASTAttrInterfacesIncGen LINK_LIBS clangAST diff --git a/clang/tools/cir-opt/CMakeLists.txt b/clang/tools/cir-opt/CMakeLists.txt index 741cdfa5950d..e99bb7c7bb84 100644 --- a/clang/tools/cir-opt/CMakeLists.txt +++ b/clang/tools/cir-opt/CMakeLists.txt @@ -13,6 +13,7 @@ set(LIBS MLIRAnalysis MLIRCIR MLIRCIRTransforms + MLIRCIRASTAttrInterfaces MLIRDialect MLIRIR MLIRMemRefDialect From 669958470c44b228018525c46cffba180431b39e Mon Sep 17 00:00:00 2001 From: Henrich Lauko Date: Mon, 28 Aug 2023 20:50:43 +0200 Subject: [PATCH 02/25] [CIR][Interfaces] Setup hierarchy for Decl interfaces. --- .../clang/CIR/Interfaces/ASTAttrInterfaces.td | 109 ++++++++++++------ 1 file changed, 74 insertions(+), 35 deletions(-) diff --git a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td index 33bef5615d0a..9f74d01b0914 100644 --- a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td +++ b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td @@ -13,52 +13,42 @@ include "mlir/IR/OpBase.td" let cppNamespace = "::mlir::cir" in { - def ASTDeclInterface : AttrInterface< "ASTDeclInterface" > { - let methods = [ - // hasAttr, cannot be templated :( - InterfaceMethod< "", "bool", "hasOwnerAttr", (ins), - /* methodBody = */ [{}], - /* defaultImplementation= */[{ - return $_attr.getDecl()->template hasAttr< clang::OwnerAttr >(); - }] - >, - InterfaceMethod< "", "bool", "hasPointerAttr", (ins), - /* methodBody = */ [{}], - /* defaultImplementation= */[{ - return $_attr.getDecl()->template hasAttr< clang::PointerAttr >(); - }] - > - ]; - } + def ASTDeclInterface : AttrInterface< "ASTDeclInterface" > {} - // def ASTNamedDeclInterface : AttrInterface< "ASTNamedDeclInterface", [ASTDeclInterface] > {} + def ASTDeclContextInterface : AttrInterface< "ASTDeclContextInterface" > {} - // def ASTTypeDeclInterface : AttrInterface< "ASTTypeDeclInterface", [ASTNamedDeclInterface] > {} + def ASTNamedDeclInterface : AttrInterface< "ASTNamedDeclInterface", + [ASTDeclInterface] + > {} - // def ASTTagDeclInterface : AttrInterface< "ASTTagDeclInterface", [ASTTypeDeclInterface] > {} + def ASTValueDeclInterface : AttrInterface< "ASTValueDeclInterface", + [ASTNamedDeclInterface] + > {} - // def ASTRecordDeclInterface : AttrInterface< "ASTRecordDeclInterface", [ASTTagDeclInterface] > {} + def ASTDeclaratorDeclInterface : AttrInterface< "ASTDeclaratorDeclInterface", + [ASTValueDeclInterface] + > {} - def ASTCXXRecorDeclInterface : AttrInterface< "ASTCXXRecorDeclInterface", [ASTDeclInterface] > { + def ASTVarDeclInterface : AttrInterface< "ASTVarDeclInterface", + [ASTDeclaratorDeclInterface] + > {} + + def ASTFunctionDeclInterface : AttrInterface< "ASTFunctionDeclInterface", + [ASTDeclaratorDeclInterface, ASTDeclContextInterface] + > { let methods = [ - InterfaceMethod< "", "bool", "isLambda", (ins), [{}], + InterfaceMethod< "", "bool", "isOverloadedOperator", (ins), [{}], /*defaultImplementation=*/ [{ - return $_attr.getDecl()->isLambda(); + return $_attr.getDecl()->isOverloadedOperator(); }] > ]; } - def ASTVarDeclInterface : AttrInterface< "ASTVarDeclInterface", [ASTDeclInterface] > {} - - def ASTFunctionDeclInterface : AttrInterface< "ASTFunctionDeclInterface", [ASTDeclInterface] > { + def ASTCXXMethodDeclInterface : AttrInterface< "ASTCXXMethodDeclInterface", + [ASTFunctionDeclInterface] + > { let methods = [ - InterfaceMethod< "", "bool", "isOverloadedOperator", (ins), [{}], - /*defaultImplementation=*/ [{ - return $_attr.getDecl()->isOverloadedOperator(); - }] - >, - // TODO move to ASTMethodDeclInterface InterfaceMethod< "", "bool", "isCopyAssignmentOperator", (ins), [{}], /*defaultImplementation=*/ [{ if (auto decl = dyn_cast< clang::CXXMethodDecl >($_attr.getDecl())) @@ -66,7 +56,6 @@ let cppNamespace = "::mlir::cir" in { return false; }] >, - // TODO move to ASTMethodDeclInterface InterfaceMethod< "", "bool", "isMoveAssignmentOperator", (ins), [{}], /*defaultImplementation=*/ [{ if (auto decl = dyn_cast< clang::CXXMethodDecl >($_attr.getDecl())) @@ -77,7 +66,57 @@ let cppNamespace = "::mlir::cir" in { ]; } - def ASTCXXMethodDeclInterface : AttrInterface< "ASTCXXMethodDeclInterface", [ASTDeclInterface] > {} + def ASTCXXConstructDeclInterface : AttrInterface< "ASTCXXConstructDeclInterface", + [ASTCXXMethodDeclInterface] + > {} + + def ASTCXXConversionDeclInterface : AttrInterface< "ASTCXXConversionDeclInterface", + [ASTCXXMethodDeclInterface] + > {} + + def ASTCXXDestructDeclInterface : AttrInterface< "ASTCXXDestructDeclInterface", + [ASTCXXMethodDeclInterface] + > {} + + def ASTTypeDeclInterface : AttrInterface< "ASTTypeDeclInterface", + [ASTNamedDeclInterface] + > {} + + def ASTTagDeclInterface : AttrInterface< "ASTTagDeclInterface", + [ASTTypeDeclInterface, ASTDeclContextInterface] + > {} + + def ASTEnumDeclInterface : AttrInterface< "ASTEnumDeclInterface", + [ASTTagDeclInterface] + > {} + + def ASTRecordDeclInterface : AttrInterface< "ASTRecordDeclInterface", + [ASTTagDeclInterface] + > {} + + def ASTCXXRecorDeclInterface : AttrInterface< "ASTCXXRecorDeclInterface", + [ASTDeclInterface] + > { + let methods = [ + InterfaceMethod< "", "bool", "isLambda", (ins), [{}], + /*defaultImplementation=*/ [{ + return $_attr.getDecl()->isLambda(); + }] + > + ]; + } + + def ASTClassTemplateSpecializationDeclInterface + : AttrInterface< "ASTClassTemplateSpecializationDeclInterface", + [ASTCXXRecorDeclInterface] + > + {} + + def ASTClassTemplatePartialSpecializationDeclInterface + : AttrInterface< "ASTClassTemplatePartialSpecializationDeclInterface", + [ASTClassTemplateSpecializationDeclInterface] + > + {} } #endif // MLIR_CIR_INTERFACES_AST_ATTR_INTERFACES From 85d4d623fdf8d7b6ba2582874e89de8f463c955d Mon Sep 17 00:00:00 2001 From: Henrich Lauko Date: Mon, 28 Aug 2023 21:13:44 +0200 Subject: [PATCH 03/25] [CIR] Include AST interfaces into dialect. --- clang/include/clang/CIR/Dialect/IR/CIRAttrs.h | 2 ++ clang/include/clang/CIR/Dialect/IR/CIRAttrs.td | 3 +++ clang/include/clang/CIR/Dialect/IR/CIROps.td | 2 ++ clang/lib/CIR/Dialect/IR/CIRAttrs.cpp | 1 + 4 files changed, 8 insertions(+) diff --git a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.h b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.h index c11246306763..624ee7b5e417 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.h +++ b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.h @@ -18,6 +18,8 @@ #include "clang/CIR/Dialect/IR/CIROpsEnums.h" +#include "clang/CIR/Interfaces/ASTAttrInterfaces.h" + //===----------------------------------------------------------------------===// // CIR Dialect Attrs //===----------------------------------------------------------------------===// diff --git a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td index f91dea2db856..cd724dfd1255 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td @@ -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 //===----------------------------------------------------------------------===// diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index 64c1a15fbc5d..edf138ae22eb 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -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" diff --git a/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp b/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp index 81f51a7976d4..aaf97805d3d6 100644 --- a/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp @@ -30,6 +30,7 @@ // ClangIR holds back AST references when available. #include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" static void printStructMembers(mlir::AsmPrinter &p, mlir::ArrayAttr members); From e841f46c5915472e2252793fb7acf79e1ea6d465 Mon Sep 17 00:00:00 2001 From: Henrich Lauko Date: Mon, 28 Aug 2023 21:27:42 +0200 Subject: [PATCH 04/25] [CIR] Fix default implementation of AST interfaces. --- clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td index 9f74d01b0914..16441da10c83 100644 --- a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td +++ b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td @@ -39,7 +39,7 @@ let cppNamespace = "::mlir::cir" in { let methods = [ InterfaceMethod< "", "bool", "isOverloadedOperator", (ins), [{}], /*defaultImplementation=*/ [{ - return $_attr.getDecl()->isOverloadedOperator(); + return $_attr.getAstDecl()->isOverloadedOperator(); }] > ]; @@ -51,14 +51,14 @@ let cppNamespace = "::mlir::cir" in { let methods = [ InterfaceMethod< "", "bool", "isCopyAssignmentOperator", (ins), [{}], /*defaultImplementation=*/ [{ - if (auto decl = dyn_cast< clang::CXXMethodDecl >($_attr.getDecl())) + if (auto decl = dyn_cast< clang::CXXMethodDecl >($_attr.getAstDecl())) return decl->isCopyAssignmentOperator(); return false; }] >, InterfaceMethod< "", "bool", "isMoveAssignmentOperator", (ins), [{}], /*defaultImplementation=*/ [{ - if (auto decl = dyn_cast< clang::CXXMethodDecl >($_attr.getDecl())) + if (auto decl = dyn_cast< clang::CXXMethodDecl >($_attr.getAstDecl())) return decl->isMoveAssignmentOperator(); return false; }] @@ -100,7 +100,7 @@ let cppNamespace = "::mlir::cir" in { let methods = [ InterfaceMethod< "", "bool", "isLambda", (ins), [{}], /*defaultImplementation=*/ [{ - return $_attr.getDecl()->isLambda(); + return $_attr.getAstDecl()->isLambda(); }] > ]; From f33288a4ac9c0287e9f09e913e614524b9ec6e0f Mon Sep 17 00:00:00 2001 From: Henrich Lauko Date: Mon, 28 Aug 2023 21:29:16 +0200 Subject: [PATCH 05/25] [CIR][IR] Introduce AST type attributes mimicing its interfaces. --- .../include/clang/CIR/Dialect/IR/CIRAttrs.td | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td index cd724dfd1255..31d65e2c0f46 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td @@ -399,10 +399,40 @@ class ASTDecl traits = []> let genVerifyDecl = 1; } -def ASTFunctionDeclAttr : ASTDecl<"FunctionDecl", "fndecl">; +def ASTFunctionDeclAttr : ASTDecl<"FunctionDecl", "fn.decl">; def ASTVarDeclAttr : ASTDecl<"VarDecl", "vardecl">; -def ASTRecordDeclAttr : ASTDecl<"RecordDecl", "recdecl">; +def ASTTypeDeclAttr: ASTDecl<"TypeDecl", "type.decl", + [ASTTypeDeclInterface] +>; + +def ASTTagDeclAttr : ASTDecl<"TagDecl", "tag.decl", + [ASTTagDeclInterface] +>; + +def ASTenumDeclAttr : ASTDecl<"EnumDecl", "enum.decl", + [ASTEnumDeclInterface] +>; + +def ASTRecordDeclAttr : ASTDecl<"RecordDecl", "rec.decl", + [ASTRecordDeclInterface] +>; + +def ASTCXXRecordDeclAttr : ASTDecl<"CXXRecordDecl", "cxxrec.decl", + [ASTCXXRecorDeclInterface] +>; + +def ASTClassTemplateSpecializationDeclAttr + : ASTDecl<"ClassTemplateSpecializationDecl" + , "cls.template.spec.decl" + , [ASTClassTemplateSpecializationDeclInterface] +>; + +def ASTClassTemplatePartialSpecializationDeclAttr + : ASTDecl<"ClassTemplatePartialSpecializationDecl" + , "cls.template.partial.spec.decl" + , [ASTClassTemplatePartialSpecializationDeclInterface] +>; //===----------------------------------------------------------------------===// // ExtraFuncAttr From f1ba4e8dc2ed56696da5f6a69aa69361a16f7f05 Mon Sep 17 00:00:00 2001 From: Henrich Lauko Date: Fri, 1 Sep 2023 15:55:33 +0200 Subject: [PATCH 06/25] [CIR][IR] Fix names and clean up interface. --- clang/include/clang/CIR/Dialect/IR/CIRAttrs.h | 2 + .../include/clang/CIR/Dialect/IR/CIRAttrs.td | 46 ++++++++++++++++--- clang/lib/CIR/Dialect/IR/CIRAttrs.cpp | 34 ++++++++++++++ 3 files changed, 75 insertions(+), 7 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.h b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.h index 624ee7b5e417..d72fc13ba60d 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.h +++ b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.h @@ -16,6 +16,8 @@ #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" diff --git a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td index 31d65e2c0f46..8a66f982c8b7 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td @@ -397,10 +397,42 @@ class ASTDecl traits = []> // Enable verifier. let genVerifyDecl = 1; + + let extraClassDeclaration = [{ + llvm::SmallVector< ASTCXXRecordDeclInterface, 4 > CXXRecordDecls() const; + }]; + + let extraClassDefinition = [{ + llvm::SmallVector< ASTCXXRecordDeclInterface, 4 > + $cppClass::CXXRecordDecls() const { + return getCXXRecordDecls(getAstDecl(), getContext()); + } + + ::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", "fn.decl">; -def ASTVarDeclAttr : ASTDecl<"VarDecl", "vardecl">; +def ASTDeclAttr : ASTDecl<"Decl", "decl", + [ASTDeclInterface] +>; + +def ASTFunctionDeclAttr : ASTDecl<"FunctionDecl", "function.decl">; +def ASTVarDeclAttr : ASTDecl<"VarDecl", "var.decl">; def ASTTypeDeclAttr: ASTDecl<"TypeDecl", "type.decl", [ASTTypeDeclInterface] @@ -414,23 +446,23 @@ def ASTenumDeclAttr : ASTDecl<"EnumDecl", "enum.decl", [ASTEnumDeclInterface] >; -def ASTRecordDeclAttr : ASTDecl<"RecordDecl", "rec.decl", +def ASTRecordDeclAttr : ASTDecl<"RecordDecl", "record.decl", [ASTRecordDeclInterface] >; -def ASTCXXRecordDeclAttr : ASTDecl<"CXXRecordDecl", "cxxrec.decl", - [ASTCXXRecorDeclInterface] +def ASTCXXRecordDeclAttr : ASTDecl<"CXXRecordDecl", "cxxrecord.decl", + [ASTCXXRecordDeclInterface] >; def ASTClassTemplateSpecializationDeclAttr : ASTDecl<"ClassTemplateSpecializationDecl" - , "cls.template.spec.decl" + , "class.template.spec.decl" , [ASTClassTemplateSpecializationDeclInterface] >; def ASTClassTemplatePartialSpecializationDeclAttr : ASTDecl<"ClassTemplatePartialSpecializationDecl" - , "cls.template.partial.spec.decl" + , "class.template.partial.spec.decl" , [ASTClassTemplatePartialSpecializationDeclInterface] >; diff --git a/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp b/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp index aaf97805d3d6..3133bbc3f8ec 100644 --- a/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp @@ -37,12 +37,46 @@ static void printStructMembers(mlir::AsmPrinter &p, static mlir::ParseResult parseStructMembers(::mlir::AsmParser &parser, mlir::ArrayAttr &members); +llvm::SmallVector< mlir::cir::ASTCXXRecordDeclInterface, 4 > +getCXXRecordDecls(const clang::DeclContext *ast, mlir::MLIRContext *ctx); + +template< typename DeclType > +llvm::SmallVector< mlir::cir::ASTCXXRecordDeclInterface, 4 > +getCXXRecordDecls(const DeclType *ast, mlir::MLIRContext *ctx); + #define GET_ATTRDEF_CLASSES #include "clang/CIR/Dialect/IR/CIROpsAttributes.cpp.inc" using namespace mlir; using namespace mlir::cir; +//===----------------------------------------------------------------------===// +// CIR AST Attr helpers +//===----------------------------------------------------------------------===// + +llvm::SmallVector< ASTCXXRecordDeclInterface, 4 > +getCXXRecordDecls(const clang::DeclContext *ast, MLIRContext *ctx) { + llvm::SmallVector< ASTCXXRecordDeclInterface, 4 > records; + + for (auto sub : ast->decls()) { + if (auto rec = clang::dyn_cast< clang::CXXRecordDecl >(sub)) { + records.emplace_back( + ASTCXXRecordDeclAttr::get(ctx, rec) + ); + } + } + + return records; +} + +template< typename DeclType > +llvm::SmallVector< ASTCXXRecordDeclInterface, 4 > +getCXXRecordDecls(const DeclType *ast, MLIRContext *ctx) { + if (auto decl = llvm::dyn_cast< clang::DeclContext >(ast)) + return getCXXRecordDecls(decl, ctx); + return {}; +} + //===----------------------------------------------------------------------===// // General CIR parsing / printing //===----------------------------------------------------------------------===// From fc057c9dabc74d27a3e0dce67793076a3d1c862f Mon Sep 17 00:00:00 2001 From: Henrich Lauko Date: Fri, 1 Sep 2023 15:56:27 +0200 Subject: [PATCH 07/25] [CIR] Fix recursive cmake dependencies of attr interfaces. --- clang/lib/CIR/CodeGen/CMakeLists.txt | 1 - clang/lib/CIR/Dialect/Transforms/CMakeLists.txt | 4 +++- clang/lib/CIR/Interfaces/CMakeLists.txt | 3 --- clang/tools/cir-opt/CMakeLists.txt | 1 - 4 files changed, 3 insertions(+), 6 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CMakeLists.txt b/clang/lib/CIR/CodeGen/CMakeLists.txt index a379ed464316..8deb7caf45bf 100644 --- a/clang/lib/CIR/CodeGen/CMakeLists.txt +++ b/clang/lib/CIR/CodeGen/CMakeLists.txt @@ -38,7 +38,6 @@ add_clang_library(clangCIR DEPENDS MLIRCIR MLIRCIROpsIncGen - MLIRCIRASTAttrInterfacesIncGen ${dialect_libs} LINK_LIBS diff --git a/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt b/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt index 880542f6d889..82952f42a2d2 100644 --- a/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt +++ b/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt @@ -13,7 +13,9 @@ add_clang_library(MLIRCIRTransforms MLIRAnalysis MLIRIR - MLIRCIR MLIRPass MLIRTransformUtils + + MLIRCIR + MLIRCIRASTAttrInterfaces ) diff --git a/clang/lib/CIR/Interfaces/CMakeLists.txt b/clang/lib/CIR/Interfaces/CMakeLists.txt index 48acb88587b6..3f41389807d7 100644 --- a/clang/lib/CIR/Interfaces/CMakeLists.txt +++ b/clang/lib/CIR/Interfaces/CMakeLists.txt @@ -8,10 +8,7 @@ add_clang_library(MLIRCIRASTAttrInterfaces MLIRCIRASTAttrInterfacesIncGen LINK_LIBS - clangCIR ${dialect_libs} - MLIRCIR - MLIRAnalysis MLIRIR MLIRSupport ) diff --git a/clang/tools/cir-opt/CMakeLists.txt b/clang/tools/cir-opt/CMakeLists.txt index e99bb7c7bb84..741cdfa5950d 100644 --- a/clang/tools/cir-opt/CMakeLists.txt +++ b/clang/tools/cir-opt/CMakeLists.txt @@ -13,7 +13,6 @@ set(LIBS MLIRAnalysis MLIRCIR MLIRCIRTransforms - MLIRCIRASTAttrInterfaces MLIRDialect MLIRIR MLIRMemRefDialect From c9480953a966cf00cf4833801da8a7ef31fbde75 Mon Sep 17 00:00:00 2001 From: Henrich Lauko Date: Fri, 1 Sep 2023 15:58:02 +0200 Subject: [PATCH 08/25] [CIR] Add AST interface methods to access record information. --- .../clang/CIR/Interfaces/ASTAttrInterfaces.h | 16 +++++++ .../clang/CIR/Interfaces/ASTAttrInterfaces.td | 44 ++++++++++++++++--- .../lib/CIR/Interfaces/ASTAttrInterfaces.cpp | 4 ++ 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h index 10f07278fd7a..a22657ae2761 100644 --- a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h +++ b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h @@ -12,8 +12,24 @@ #include "mlir/IR/Attributes.h" #include "clang/AST/Attr.h" +#include "clang/AST/DeclTemplate.h" /// Include the generated interface declarations. #include "clang/CIR/Interfaces/ASTAttrInterfaces.h.inc" +namespace mlir::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(); + } + } + +} // namespace mlir::cir + #endif // MLIR_INTERFACES_CIR_AST_ATR_INTERFACES_H_ diff --git a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td index 16441da10c83..8f9ebb3db35d 100644 --- a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td +++ b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td @@ -13,13 +13,44 @@ include "mlir/IR/OpBase.td" let cppNamespace = "::mlir::cir" in { - def ASTDeclInterface : AttrInterface< "ASTDeclInterface" > {} + def ASTDeclInterface : AttrInterface< "ASTDeclInterface" > { + let methods = [ + InterfaceMethod< "", "bool", "hasOwnerAttr", (ins), [{}], + /*defaultImplementation=*/ [{ + return $_attr.getAstDecl()->template hasAttr< clang::OwnerAttr >(); + }] + >, + InterfaceMethod< "", "bool", "hasPointerAttr", (ins), [{}], + /*defaultImplementation=*/ [{ + return $_attr.getAstDecl()->template hasAttr< clang::PointerAttr >(); + }] + >, + InterfaceMethod< "", "ASTDeclInterface", "getNextDeclInContext", (ins), [{}], + /*defaultImplementation=*/ [{ + return $_attr.getNextDeclInContext(); + }] + > + ]; + } def ASTDeclContextInterface : AttrInterface< "ASTDeclContextInterface" > {} def ASTNamedDeclInterface : AttrInterface< "ASTNamedDeclInterface", [ASTDeclInterface] - > {} + > { + let methods = [ + InterfaceMethod< "", "clang::DeclarationName", "getDeclName", (ins), [{}], + /*defaultImplementation=*/ [{ + return $_attr.getAstDecl()->getDeclName(); + }] + >, + InterfaceMethod< "", "llvm::StringRef", "getName", (ins), [{}], + /*defaultImplementation=*/ [{ + return $_attr.getAstDecl()->getName(); + }] + > + ]; + } def ASTValueDeclInterface : AttrInterface< "ASTValueDeclInterface", [ASTNamedDeclInterface] @@ -94,21 +125,24 @@ let cppNamespace = "::mlir::cir" in { [ASTTagDeclInterface] > {} - def ASTCXXRecorDeclInterface : AttrInterface< "ASTCXXRecorDeclInterface", - [ASTDeclInterface] + def ASTCXXRecordDeclInterface : AttrInterface< "ASTCXXRecordDeclInterface", + [ASTRecordDeclInterface] > { let methods = [ InterfaceMethod< "", "bool", "isLambda", (ins), [{}], /*defaultImplementation=*/ [{ return $_attr.getAstDecl()->isLambda(); }] + >, + InterfaceMethod< "", + "llvm::SmallVector< ASTCXXRecordDeclInterface, 4 >", "CXXRecordDecls" > ]; } def ASTClassTemplateSpecializationDeclInterface : AttrInterface< "ASTClassTemplateSpecializationDeclInterface", - [ASTCXXRecorDeclInterface] + [ASTCXXRecordDeclInterface] > {} diff --git a/clang/lib/CIR/Interfaces/ASTAttrInterfaces.cpp b/clang/lib/CIR/Interfaces/ASTAttrInterfaces.cpp index 8dd24b7384b6..a3f525dd65a3 100644 --- a/clang/lib/CIR/Interfaces/ASTAttrInterfaces.cpp +++ b/clang/lib/CIR/Interfaces/ASTAttrInterfaces.cpp @@ -7,5 +7,9 @@ //===----------------------------------------------------------------------===// #include "clang/CIR/Interfaces/ASTAttrInterfaces.h" +#include "llvm/ADT/SmallVector.h" + +using namespace mlir::cir; + /// Include the generated type qualifiers interfaces. #include "clang/CIR/Interfaces/ASTAttrInterfaces.cpp.inc" From 0a046c5711860da87efdb623d1265cbeed2d44e5 Mon Sep 17 00:00:00 2001 From: Henrich Lauko Date: Fri, 1 Sep 2023 15:59:23 +0200 Subject: [PATCH 09/25] [CIR][IR] Let StructType use AST arbitrary attribute. --- clang/include/clang/CIR/Dialect/IR/CIRTypes.h | 8 ++------ clang/include/clang/CIR/Dialect/IR/CIRTypes.td | 5 +++-- clang/lib/CIR/CodeGen/CIRGenBuilder.h | 4 ++-- clang/lib/CIR/Dialect/IR/CIRTypes.cpp | 6 +++--- 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.h b/clang/include/clang/CIR/Dialect/IR/CIRTypes.h index 87aea83b744e..1286225f04aa 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.h +++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.h @@ -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" diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td index 88087f8915ad..59aa4120608c 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td @@ -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" @@ -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 + OptionalParameter<"Attribute">:$ast ); let hasCustomAssemblyFormat = 1; @@ -163,7 +164,7 @@ def CIR_StructType : CIR_Type<"Struct", "struct", let extraClassDefinition = [{ void $cppClass::dropAst() { - getImpl()->ast = std::nullopt; + getImpl()->ast = Attribute(); } }]; } diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.h b/clang/lib/CIR/CodeGen/CIRGenBuilder.h index a52ad79a500e..68bc26a6518d 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuilder.h +++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.h @@ -170,8 +170,8 @@ class CIRGenBuilderTy : public mlir::OpBuilder { if (!structTy) structTy = getType( members, mlir::StringAttr::get(getContext()), - /*body=*/true, packed, mlir::cir::StructType::Struct, - /*ast=*/std::nullopt); + /*body=*/true, packed, mlir::cir::StructType::Struct + /*ast=*/mlir::Attribute()); // Return zero or anonymous constant struct. if (isZero) diff --git a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp index 4ea4ea482e2f..a2a6803ee03e 100644 --- a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp @@ -156,7 +156,7 @@ Type StructType::parse(mlir::AsmParser &parser) { return {}; return StructType::get(parser.getContext(), members, id, body, packed, kind, - std::nullopt); + mlir::Attribute()); } void StructType::print(mlir::AsmPrinter &printer) const { @@ -187,9 +187,9 @@ void StructType::print(mlir::AsmPrinter &printer) const { printer << "}"; } - if (getAst().has_value()) { + if (getAst()) { printer << " "; - printer.printAttribute(getAst().value()); + printer.printAttribute(getAst()); } printer << '>'; From 7a9c0e77b1e0f5c2ebfcf2f14dae689e628c182b Mon Sep 17 00:00:00 2001 From: Henrich Lauko Date: Fri, 1 Sep 2023 16:00:51 +0200 Subject: [PATCH 10/25] [CIR][CIRGen] Use ASTCXXRecordDeclAttr in record builder. --- clang/lib/CIR/CodeGen/CIRRecordLayoutBuilder.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/clang/lib/CIR/CodeGen/CIRRecordLayoutBuilder.cpp b/clang/lib/CIR/CodeGen/CIRRecordLayoutBuilder.cpp index f91e4b6a1af6..ec80cbf50c01 100644 --- a/clang/lib/CIR/CodeGen/CIRRecordLayoutBuilder.cpp +++ b/clang/lib/CIR/CodeGen/CIRRecordLayoutBuilder.cpp @@ -606,7 +606,9 @@ CIRGenTypes::computeRecordLayout(const RecordDecl *D, CIRRecordLowering baseBuilder(*this, D, /*Packed=*/builder.isPacked); auto baseIdentifier = getRecordTypeName(D, ".base"); *BaseTy = Builder.getStructTy(baseBuilder.fieldTypes, baseIdentifier, - /*body=*/true, /*packed=*/false, D); + /*body=*/true, /*packed=*/false, + mlir::cir::ASTCXXRecordDeclAttr::get(&getMLIRContext(), + llvm::dyn_cast(D))); // TODO(cir): add something like addRecordTypeName // BaseTy and Ty must agree on their packedness for getCIRFieldNo to work From 7b1400572281a6854890d25aa650951947ca9e24 Mon Sep 17 00:00:00 2001 From: Henrich Lauko Date: Fri, 1 Sep 2023 16:01:39 +0200 Subject: [PATCH 11/25] [CIR] Remove generated attribute printers and parsers. --- clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 54 ------------------------- 1 file changed, 54 deletions(-) diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp index d46a643edf3f..a34aa3148c6b 100644 --- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp @@ -2306,60 +2306,6 @@ void SignedOverflowBehaviorAttr::print(::mlir::AsmPrinter &printer) const { printer << ">"; } -::mlir::Attribute ASTFunctionDeclAttr::parse(::mlir::AsmParser &parser, - ::mlir::Type type) { - // We cannot really parse anything AST related at this point since we have no - // serialization/JSON story. Even if the attr is parsed, it just holds nullptr - // instead of the AST node. - return get(parser.getContext(), nullptr); -} - -void ASTFunctionDeclAttr::print(::mlir::AsmPrinter &printer) const { - // Nothing to print besides the mnemonics. -} - -LogicalResult ASTFunctionDeclAttr::verify( - ::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError, - const ::clang::FunctionDecl *decl) { - return success(); -} - -::mlir::Attribute ASTVarDeclAttr::parse(::mlir::AsmParser &parser, - ::mlir::Type type) { - // We cannot really parse anything AST related at this point since we have no - // serialization/JSON story. Even if the attr is parsed, it just holds nullptr - // instead of the AST node. - return get(parser.getContext(), nullptr); -} - -void ASTVarDeclAttr::print(::mlir::AsmPrinter &printer) const { - // Nothing to print besides the mnemonics. -} - -LogicalResult ASTVarDeclAttr::verify( - ::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError, - const ::clang::VarDecl *decl) { - return success(); -} - -::mlir::Attribute ASTRecordDeclAttr::parse(::mlir::AsmParser &parser, - ::mlir::Type type) { - // We cannot really parse anything AST related at this point since we have no - // serialization/JSON story. Even if the attr is parsed, it just holds nullptr - // instead of the AST node. - return get(parser.getContext(), nullptr); -} - -void ASTRecordDeclAttr::print(::mlir::AsmPrinter &printer) const { - // Nothing to print besides the mnemonics. -} - -LogicalResult ASTRecordDeclAttr::verify( - ::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError, - const ::clang::RecordDecl *decl) { - return success(); -} - LogicalResult TypeInfoAttr::verify( ::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError, ::mlir::Type type, ::mlir::ArrayAttr typeinfoData) { From 563248eeb9838981557ba3a676d733143df0b6f8 Mon Sep 17 00:00:00 2001 From: Henrich Lauko Date: Fri, 1 Sep 2023 16:03:33 +0200 Subject: [PATCH 12/25] [CIR][Lifetime] Use ASTDeclInterface instead of direct AST access. --- .../CIR/Dialect/Transforms/LifetimeCheck.cpp | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp b/clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp index 3bbb91b5b3a1..64f84024149a 100644 --- a/clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp +++ b/clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp @@ -895,9 +895,10 @@ template bool isStructAndHasAttr(mlir::Type ty) { if (!ty.isa()) return false; auto sTy = ty.cast(); - auto recordDecl = sTy.getAst()->getAstDecl(); - if (recordDecl->hasAttr()) - return true; + auto recordDecl = sTy.getAst(); + if (auto interface = dyn_cast< ASTDeclInterface >(recordDecl)) + if (hasAttr(interface)) + return true; return false; } @@ -1765,9 +1766,9 @@ bool LifetimeCheckPass::isLambdaType(mlir::Type ty) { auto taskTy = ty.dyn_cast(); if (!taskTy) return false; - auto recordDecl = taskTy.getAst()->getAstDecl(); - if (recordDecl->isLambda()) - IsLambdaTyCache[ty] = true; + if (auto recordDecl = dyn_cast< ASTCXXRecordDeclInterface >(taskTy.getAst())) + if (recordDecl.isLambda()) + IsLambdaTyCache[ty] = true; return IsLambdaTyCache[ty]; } @@ -1781,15 +1782,14 @@ bool LifetimeCheckPass::isTaskType(mlir::Value taskVal) { auto taskTy = taskVal.getType().dyn_cast(); if (!taskTy) return false; - auto recordDecl = taskTy.getAst()->getAstDecl(); - auto *spec = dyn_cast(recordDecl); + auto recordDecl = taskTy.getAst(); + // FIXME we are not emmiting ASTClassTemplateSpecializationDeclInterface anywhere + auto spec = dyn_cast< ASTClassTemplateSpecializationDeclInterface >(recordDecl); if (!spec) return false; - for (auto *sub : spec->decls()) { - auto *subRec = dyn_cast(sub); - if (subRec && subRec->getDeclName().isIdentifier() && - subRec->getName() == "promise_type") { + for (auto subRec : spec.CXXRecordDecls()) { + if (subRec.getDeclName().isIdentifier() && subRec.getName() == "promise_type") { IsTaskTyCache[ty] = true; break; } From 6305a99ffffbb0fdd9bd4c0d36593683e03c0534 Mon Sep 17 00:00:00 2001 From: Henrich Lauko Date: Fri, 1 Sep 2023 16:04:36 +0200 Subject: [PATCH 13/25] [CIR] Update expected AST attributes names in tests. --- clang/test/CIR/CodeGen/bitfields.cpp | 2 +- clang/test/CIR/CodeGen/dtors.cpp | 2 +- clang/test/CIR/CodeGen/static.cpp | 8 ++++---- clang/test/CIR/CodeGen/struct.cpp | 2 +- clang/test/CIR/CodeGen/union.cpp | 12 ++++++------ clang/test/CIR/CodeGen/vtable-rtti.cpp | 2 +- clang/test/CIR/IR/global.cir | 2 +- clang/test/CIR/IR/invalid.cir | 4 ++-- clang/test/CIR/IR/struct.cir | 2 +- clang/test/CIR/Lowering/array.cir | 8 ++++---- clang/test/CIR/Lowering/globals.cir | 8 ++++---- clang/test/CIR/Lowering/struct.cir | 8 ++++---- clang/test/CIR/Lowering/unions.cir | 6 +++--- clang/test/CIR/Lowering/variadics.cir | 2 +- 14 files changed, 34 insertions(+), 34 deletions(-) diff --git a/clang/test/CIR/CodeGen/bitfields.cpp b/clang/test/CIR/CodeGen/bitfields.cpp index c1f801af1e7c..0cf8029b04d4 100644 --- a/clang/test/CIR/CodeGen/bitfields.cpp +++ b/clang/test/CIR/CodeGen/bitfields.cpp @@ -14,5 +14,5 @@ void m() { __long l; } -// CHECK: !ty_22anon22 = !cir.struct +// CHECK: !ty_22anon22 = !cir.struct // CHECK: !ty_22__long22 = !cir.struct}> diff --git a/clang/test/CIR/CodeGen/dtors.cpp b/clang/test/CIR/CodeGen/dtors.cpp index 7e59a8ec7fbe..d19bd90f5ac7 100644 --- a/clang/test/CIR/CodeGen/dtors.cpp +++ b/clang/test/CIR/CodeGen/dtors.cpp @@ -37,7 +37,7 @@ class B : public A }; // Class A -// CHECK: ![[ClassA:ty_.*]] = !cir.struct>>} #cir.recdecl.ast> +// CHECK: ![[ClassA:ty_.*]] = !cir.struct>>} #cir.cxxrecord.decl.ast> // Class B // CHECK: ![[ClassB:ty_.*]] = !cir.struct diff --git a/clang/test/CIR/CodeGen/static.cpp b/clang/test/CIR/CodeGen/static.cpp index d0620e77b050..2d6c892f7028 100644 --- a/clang/test/CIR/CodeGen/static.cpp +++ b/clang/test/CIR/CodeGen/static.cpp @@ -26,7 +26,7 @@ static Init __ioinit2(false); // BEFORE-NEXT: } dtor { // BEFORE-NEXT: %0 = cir.get_global @_ZL8__ioinit : cir.ptr // BEFORE-NEXT: cir.call @_ZN4InitD1Ev(%0) : (!cir.ptr) -> () -// BEFORE-NEXT: } {ast = #cir.vardecl.ast} +// BEFORE-NEXT: } {ast = #cir.var.decl.ast} // BEFORE: cir.global "private" internal @_ZL9__ioinit2 = ctor : !ty_22Init22 { // BEFORE-NEXT: %0 = cir.get_global @_ZL9__ioinit2 : cir.ptr // BEFORE-NEXT: %1 = cir.const(#false) : !cir.bool @@ -34,7 +34,7 @@ static Init __ioinit2(false); // BEFORE-NEXT: } dtor { // BEFORE-NEXT: %0 = cir.get_global @_ZL9__ioinit2 : cir.ptr // BEFORE-NEXT: cir.call @_ZN4InitD1Ev(%0) : (!cir.ptr) -> () -// BEFORE-NEXT: } {ast = #cir.vardecl.ast} +// BEFORE-NEXT: } {ast = #cir.var.decl.ast} // BEFORE-NEXT: } @@ -43,7 +43,7 @@ static Init __ioinit2(false); // AFTER-NEXT: cir.func private @__cxa_atexit(!cir.ptr)>>, !cir.ptr, !cir.ptr) // AFTER-NEXT: cir.func private @_ZN4InitC1Eb(!cir.ptr, !cir.bool) // AFTER-NEXT: cir.func private @_ZN4InitD1Ev(!cir.ptr) -// AFTER-NEXT: cir.global "private" internal @_ZL8__ioinit = #cir.zero : !ty_22Init22 {ast = #cir.vardecl.ast} +// AFTER-NEXT: cir.global "private" internal @_ZL8__ioinit = #cir.zero : !ty_22Init22 {ast = #cir.var.decl.ast} // AFTER-NEXT: cir.func internal private @__cxx_global_var_init() // AFTER-NEXT: %0 = cir.get_global @_ZL8__ioinit : cir.ptr // AFTER-NEXT: %1 = cir.const(#true) : !cir.bool @@ -55,7 +55,7 @@ static Init __ioinit2(false); // AFTER-NEXT: %6 = cir.get_global @__dso_handle : cir.ptr // AFTER-NEXT: cir.call @__cxa_atexit(%4, %5, %6) : (!cir.ptr)>>, !cir.ptr, !cir.ptr) -> () // AFTER-NEXT: cir.return -// AFTER: cir.global "private" internal @_ZL9__ioinit2 = #cir.zero : !ty_22Init22 {ast = #cir.vardecl.ast} +// AFTER: cir.global "private" internal @_ZL9__ioinit2 = #cir.zero : !ty_22Init22 {ast = #cir.var.decl.ast} // AFTER-NEXT: cir.func internal private @__cxx_global_var_init.1() // AFTER-NEXT: %0 = cir.get_global @_ZL9__ioinit2 : cir.ptr // AFTER-NEXT: %1 = cir.const(#false) : !cir.bool diff --git a/clang/test/CIR/CodeGen/struct.cpp b/clang/test/CIR/CodeGen/struct.cpp index 55b1a4dd725b..4ef84aa2ed48 100644 --- a/clang/test/CIR/CodeGen/struct.cpp +++ b/clang/test/CIR/CodeGen/struct.cpp @@ -30,7 +30,7 @@ void yoyo(incomplete *i) {} // CHECK-DAG: !ty_22Bar22 = !cir.struct // CHECK-DAG: !ty_22Foo22 = !cir.struct -// CHECK-DAG: !ty_22Mandalore22 = !cir.struct, !s32i} #cir.recdecl.ast> +// CHECK-DAG: !ty_22Mandalore22 = !cir.struct, !s32i} #cir.cxxrecord.decl.ast> // CHECK-DAG: !ty_22Adv22 = !cir.struct // CHECK-DAG: !ty_22Entry22 = !cir.struct, !cir.ptr)>>}> diff --git a/clang/test/CIR/CodeGen/union.cpp b/clang/test/CIR/CodeGen/union.cpp index 8892157a6824..58383a0e2442 100644 --- a/clang/test/CIR/CodeGen/union.cpp +++ b/clang/test/CIR/CodeGen/union.cpp @@ -6,10 +6,10 @@ typedef union { yolo y; struct { int lifecnt; }; } yolm; typedef union { yolo y; struct { int *lifecnt; int genpad; }; } yolm2; typedef union { yolo y; struct { bool life; int genpad; }; } yolm3; -// CHECK-DAG: !ty_22U23A3ADummy22 = !cir.struct -// CHECK-DAG: !ty_22anon221 = !cir.struct -// CHECK-DAG: !ty_22yolo22 = !cir.struct -// CHECK-DAG: !ty_22anon222 = !cir.struct, !s32i} #cir.recdecl.ast> +// CHECK-DAG: !ty_22U23A3ADummy22 = !cir.struct +// CHECK-DAG: !ty_22anon221 = !cir.struct +// CHECK-DAG: !ty_22yolo22 = !cir.struct +// CHECK-DAG: !ty_22anon222 = !cir.struct, !s32i} #cir.cxxrecord.decl.ast> // CHECK-DAG: !ty_22yolm22 = !cir.struct // CHECK-DAG: !ty_22yolm322 = !cir.struct @@ -33,14 +33,14 @@ union U2 { float f; } s; } u2; -// CHECK-DAG: !cir.struct +// CHECK-DAG: !cir.struct // Should genereate unions without padding. union U3 { short b; U u; } u3; -// CHECK-DAG: !ty_22U322 = !cir.struct +// CHECK-DAG: !ty_22U322 = !cir.struct void m() { yolm q; diff --git a/clang/test/CIR/CodeGen/vtable-rtti.cpp b/clang/test/CIR/CodeGen/vtable-rtti.cpp index 35cc4e58d75e..9830121c7848 100644 --- a/clang/test/CIR/CodeGen/vtable-rtti.cpp +++ b/clang/test/CIR/CodeGen/vtable-rtti.cpp @@ -24,7 +24,7 @@ class B : public A // CHECK: ![[VTableTypeA:ty_.*]] = !cir.struct x 5>}> // Class A -// CHECK: ![[ClassA:ty_.*]] = !cir.struct>>} #cir.recdecl.ast> +// CHECK: ![[ClassA:ty_.*]] = !cir.struct>>} #cir.cxxrecord.decl.ast> // Class B // CHECK: ![[ClassB:ty_.*]] = !cir.struct diff --git a/clang/test/CIR/IR/global.cir b/clang/test/CIR/IR/global.cir index 4ffda321f221..47a862175f42 100644 --- a/clang/test/CIR/IR/global.cir +++ b/clang/test/CIR/IR/global.cir @@ -3,7 +3,7 @@ !s8i = !cir.int !s32i = !cir.int !s64i = !cir.int -!ty_22Init22 = !cir.struct +!ty_22Init22 = !cir.struct module { cir.global external @a = #cir.int<3> : !s32i cir.global external @rgb = #cir.const_array<[#cir.int<0> : !s8i, #cir.int<-23> : !s8i, #cir.int<33> : !s8i] : !cir.array> diff --git a/clang/test/CIR/IR/invalid.cir b/clang/test/CIR/IR/invalid.cir index cd5d709e57a4..d3e653b967d8 100644 --- a/clang/test/CIR/IR/invalid.cir +++ b/clang/test/CIR/IR/invalid.cir @@ -488,7 +488,7 @@ module { // ----- !s8i = !cir.int -!ty_22Init22 = !cir.struct +!ty_22Init22 = !cir.struct module { cir.global "private" internal @_ZL8__ioinit = ctor : !ty_22Init22 { } @@ -498,7 +498,7 @@ module { // ----- !s8i = !cir.int #true = #cir.bool : !cir.bool -!ty_22Init22 = !cir.struct +!ty_22Init22 = !cir.struct module { cir.func private @_ZN4InitC1Eb(!cir.ptr) cir.global "private" internal @_ZL8__ioinit = ctor : !ty_22Init22 { diff --git a/clang/test/CIR/IR/struct.cir b/clang/test/CIR/IR/struct.cir index aa0acce60abd..0bee3430c60d 100644 --- a/clang/test/CIR/IR/struct.cir +++ b/clang/test/CIR/IR/struct.cir @@ -8,7 +8,7 @@ !ty_2222 = !cir.struct x 5>}> !ty_22221 = !cir.struct, !cir.ptr, !cir.ptr}> -!ty_22A22 = !cir.struct +!ty_22A22 = !cir.struct !ty_22i22 = !cir.struct !ty_22S22 = !cir.struct !ty_22S122 = !cir.struct diff --git a/clang/test/CIR/Lowering/array.cir b/clang/test/CIR/Lowering/array.cir index 3028835d521d..2e92f54171ee 100644 --- a/clang/test/CIR/Lowering/array.cir +++ b/clang/test/CIR/Lowering/array.cir @@ -2,7 +2,7 @@ // RUN: cir-translate %s -cir-to-llvmir -o - | FileCheck %s -check-prefix=LLVM !s32i = !cir.int -!ty_22S22 = !cir.struct +!ty_22S22 = !cir.struct module { cir.func @foo() { @@ -26,10 +26,10 @@ module { // CHECK: %0 = llvm.mlir.undef : !llvm.array<2 x struct<"struct.S", (i32)>> // CHECK: %1 = llvm.mlir.undef : !llvm.struct<"struct.S", (i32)> // CHECK: %2 = llvm.mlir.constant(1 : i32) : i32 - // CHECK: %3 = llvm.insertvalue %2, %1[0] : !llvm.struct<"struct.S", (i32)> - // CHECK: %4 = llvm.insertvalue %3, %0[0] : !llvm.array<2 x struct<"struct.S", (i32)>> + // CHECK: %3 = llvm.insertvalue %2, %1[0] : !llvm.struct<"struct.S", (i32)> + // CHECK: %4 = llvm.insertvalue %3, %0[0] : !llvm.array<2 x struct<"struct.S", (i32)>> // CHECK: %5 = cir.llvmir.zeroinit : !llvm.struct<"struct.S", (i32)> - // CHECK: %6 = llvm.insertvalue %5, %4[1] : !llvm.array<2 x struct<"struct.S", (i32)>> + // CHECK: %6 = llvm.insertvalue %5, %4[1] : !llvm.array<2 x struct<"struct.S", (i32)>> // CHECK: llvm.return %6 : !llvm.array<2 x struct<"struct.S", (i32)>> // CHECK: } } diff --git a/clang/test/CIR/Lowering/globals.cir b/clang/test/CIR/Lowering/globals.cir index a6476ff2fe1d..9920c3a4a7f9 100644 --- a/clang/test/CIR/Lowering/globals.cir +++ b/clang/test/CIR/Lowering/globals.cir @@ -10,10 +10,10 @@ !u32i = !cir.int !u64i = !cir.int !u8i = !cir.int -!ty_22A22 = !cir.struct x 2>} #cir.recdecl.ast> -!ty_22Bar22 = !cir.struct -!ty_22StringStruct22 = !cir.struct, !cir.array, !cir.array} #cir.recdecl.ast> -!ty_22StringStructPtr22 = !cir.struct} #cir.recdecl.ast> +!ty_22A22 = !cir.struct x 2>} #cir.record.decl.ast> +!ty_22Bar22 = !cir.struct +!ty_22StringStruct22 = !cir.struct, !cir.array, !cir.array} #cir.record.decl.ast> +!ty_22StringStructPtr22 = !cir.struct} #cir.record.decl.ast> module { cir.global external @a = #cir.int<3> : !s32i diff --git a/clang/test/CIR/Lowering/struct.cir b/clang/test/CIR/Lowering/struct.cir index 7543154ea0e5..2ef41da0a175 100644 --- a/clang/test/CIR/Lowering/struct.cir +++ b/clang/test/CIR/Lowering/struct.cir @@ -5,10 +5,10 @@ !u8i = !cir.int !u32i = !cir.int !ty_22S22 = !cir.struct -!ty_22S2A22 = !cir.struct -!ty_22S122 = !cir.struct} #cir.recdecl.ast> -!ty_22S222 = !cir.struct -!ty_22S322 = !cir.struct +!ty_22S2A22 = !cir.struct +!ty_22S122 = !cir.struct} #cir.cxxrecord.decl.ast> +!ty_22S222 = !cir.struct +!ty_22S322 = !cir.struct module { cir.func @test() { diff --git a/clang/test/CIR/Lowering/unions.cir b/clang/test/CIR/Lowering/unions.cir index 3e303c377f7e..c9c2b6518d4e 100644 --- a/clang/test/CIR/Lowering/unions.cir +++ b/clang/test/CIR/Lowering/unions.cir @@ -4,9 +4,9 @@ !s16i = !cir.int !s32i = !cir.int #true = #cir.bool : !cir.bool -!ty_22U122 = !cir.struct -!ty_22U222 = !cir.struct -!ty_22U322 = !cir.struct +!ty_22U122 = !cir.struct +!ty_22U222 = !cir.struct +!ty_22U322 = !cir.struct module { // Should lower union to struct with only the largest member. cir.global external @u1 = #cir.zero : !ty_22U122 diff --git a/clang/test/CIR/Lowering/variadics.cir b/clang/test/CIR/Lowering/variadics.cir index f533b54d0151..2c7c9acf5e25 100644 --- a/clang/test/CIR/Lowering/variadics.cir +++ b/clang/test/CIR/Lowering/variadics.cir @@ -5,7 +5,7 @@ !u32i = !cir.int !u8i = !cir.int -!ty_22__va_list_tag22 = !cir.struct, !cir.ptr} #cir.recdecl.ast> +!ty_22__va_list_tag22 = !cir.struct, !cir.ptr} #cir.record.decl.ast> module { cir.func @average(%arg0: !s32i, ...) -> !s32i { From c84a092013197dc7fe3408419ecf280e1486cb9b Mon Sep 17 00:00:00 2001 From: Henrich Lauko Date: Fri, 1 Sep 2023 16:31:05 +0200 Subject: [PATCH 14/25] [CIR] Replace VarDeclAttr with the corresponging interface. --- clang/include/clang/CIR/Dialect/IR/CIRAttrs.td | 5 ++++- clang/include/clang/CIR/Dialect/IR/CIROps.td | 4 ++-- .../include/clang/CIR/Interfaces/ASTAttrInterfaces.h | 1 + .../clang/CIR/Interfaces/ASTAttrInterfaces.td | 12 +++++++++++- clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp | 7 ++----- 5 files changed, 20 insertions(+), 9 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td index 8a66f982c8b7..21f79f6a6f36 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td @@ -432,7 +432,10 @@ def ASTDeclAttr : ASTDecl<"Decl", "decl", >; def ASTFunctionDeclAttr : ASTDecl<"FunctionDecl", "function.decl">; -def ASTVarDeclAttr : ASTDecl<"VarDecl", "var.decl">; + +def ASTVarDeclAttr : ASTDecl<"VarDecl", "var.decl", + [ASTVarDeclInterface] +>; def ASTTypeDeclAttr: ASTDecl<"TypeDecl", "type.decl", [ASTTypeDeclInterface] diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index edf138ae22eb..c8bb5d83ab4a 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -295,7 +295,7 @@ def AllocaOp : CIR_Op<"alloca", [ StrAttr:$name, UnitAttr:$init, ConfinedAttr, [IntMinValue<0>]>:$alignment, - OptionalAttr:$ast + OptionalAttr:$ast ); let results = (outs Res:$initial_value, UnitAttr:$constant, OptionalAttr:$alignment, - OptionalAttr:$ast + OptionalAttr:$ast ); let regions = (region AnyRegion:$ctorRegion, AnyRegion:$dtorRegion); let assemblyFormat = [{ diff --git a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h index a22657ae2761..e8e380b0ab28 100644 --- a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h +++ b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h @@ -12,6 +12,7 @@ #include "mlir/IR/Attributes.h" #include "clang/AST/Attr.h" +#include "clang/AST/Mangle.h" #include "clang/AST/DeclTemplate.h" /// Include the generated interface declarations. diff --git a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td index 8f9ebb3db35d..f3682f071b00 100644 --- a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td +++ b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td @@ -62,7 +62,17 @@ let cppNamespace = "::mlir::cir" in { def ASTVarDeclInterface : AttrInterface< "ASTVarDeclInterface", [ASTDeclaratorDeclInterface] - > {} + > { + let methods = [ + InterfaceMethod< "", "void", "mangleDynamicInitializer", (ins "llvm::raw_ostream&":$Out), [{}], + /*defaultImplementation=*/ [{ + std::unique_ptr MangleCtx( + $_attr.getAstDecl()->getASTContext().createMangleContext()); + MangleCtx->mangleDynamicInitializer($_attr.getAstDecl(), Out); + }] + > + ]; + } def ASTFunctionDeclInterface : AttrInterface< "ASTFunctionDeclInterface", [ASTDeclaratorDeclInterface, ASTDeclContextInterface] diff --git a/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp b/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp index 6ed1846bf277..c5a2ca694d9b 100644 --- a/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp +++ b/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp @@ -133,13 +133,10 @@ cir::FuncOp LoweringPreparePass::buildRuntimeFunction( } cir::FuncOp LoweringPreparePass::buildCXXGlobalVarDeclInitFunc(GlobalOp op) { - auto varDecl = op.getAst()->getAstDecl(); SmallString<256> fnName; { - std::unique_ptr MangleCtx( - astCtx->createMangleContext()); llvm::raw_svector_ostream Out(fnName); - MangleCtx->mangleDynamicInitializer(varDecl, Out); + op.getAst()->mangleDynamicInitializer(Out); // Name numbering uint32_t cnt = dynamicInitializerNames[fnName]++; if (cnt) @@ -164,7 +161,7 @@ cir::FuncOp LoweringPreparePass::buildCXXGlobalVarDeclInitFunc(GlobalOp op) { // Register the destructor call with __cxa_atexit - assert(varDecl->getTLSKind() == clang::VarDecl::TLS_None && " TLS NYI"); + assert(io.getAst()->getTLSKind() == clang::VarDecl::TLS_None && " TLS NYI"); // Create a variable that binds the atexit to this shared object. builder.setInsertionPointToStart(&theModule.getBodyRegion().front()); auto Handle = buildRuntimeVariable(builder, "__dso_handle", op.getLoc(), From 7a3b7abf9ff6064fc8d948a089734088eb99b33a Mon Sep 17 00:00:00 2001 From: Henrich Lauko Date: Fri, 1 Sep 2023 17:53:35 +0200 Subject: [PATCH 15/25] [CIR] Replace FunctionDeclAttr with the corresponging interfaces. --- .../include/clang/CIR/Dialect/IR/CIRAttrs.td | 20 ++++++- clang/include/clang/CIR/Dialect/IR/CIROps.td | 2 +- .../clang/CIR/Interfaces/ASTAttrInterfaces.td | 29 +++++++++- clang/lib/CIR/CodeGen/CIRGenModule.cpp | 16 ++++- .../CIR/Dialect/Transforms/LifetimeCheck.cpp | 58 +++++++++---------- 5 files changed, 88 insertions(+), 37 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td index 21f79f6a6f36..800e5af16b58 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td @@ -431,7 +431,25 @@ def ASTDeclAttr : ASTDecl<"Decl", "decl", [ASTDeclInterface] >; -def ASTFunctionDeclAttr : ASTDecl<"FunctionDecl", "function.decl">; +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] diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index c8bb5d83ab4a..cb97f37b4dd8 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -1592,7 +1592,7 @@ def FuncOp : CIR_Op<"func", [ OptionalAttr:$arg_attrs, OptionalAttr:$res_attrs, OptionalAttr:$aliasee, - OptionalAttr:$ast); + OptionalAttr:$ast); let regions = (region AnyRegion:$body); let skipDefaultBuilders = 1; diff --git a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td index f3682f071b00..e8677ca803fb 100644 --- a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td +++ b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td @@ -82,6 +82,11 @@ let cppNamespace = "::mlir::cir" in { /*defaultImplementation=*/ [{ return $_attr.getAstDecl()->isOverloadedOperator(); }] + >, + InterfaceMethod< "", "bool", "isStatic", (ins), [{}], + /*defaultImplementation=*/ [{ + return $_attr.getAstDecl()->isStatic(); + }] > ]; } @@ -103,19 +108,37 @@ let cppNamespace = "::mlir::cir" in { return decl->isMoveAssignmentOperator(); return false; }] + >, + InterfaceMethod< "", "bool", "isConst", (ins), [{}], + /*defaultImplementation=*/ [{ + return $_attr.getAstDecl()->isConst(); + }] > ]; } - def ASTCXXConstructDeclInterface : AttrInterface< "ASTCXXConstructDeclInterface", + def ASTCXXConstructorDeclInterface : AttrInterface< "ASTCXXConstructorDeclInterface", [ASTCXXMethodDeclInterface] - > {} + > { + let methods = [ + InterfaceMethod< "", "bool", "isDefaultConstructor", (ins), [{}], + /*defaultImplementation=*/ [{ + return $_attr.getAstDecl()->isDefaultConstructor(); + }] + >, + InterfaceMethod< "", "bool", "isCopyConstructor", (ins), [{}], + /*defaultImplementation=*/ [{ + return $_attr.getAstDecl()->isCopyConstructor(); + }] + > + ]; + } def ASTCXXConversionDeclInterface : AttrInterface< "ASTCXXConversionDeclInterface", [ASTCXXMethodDeclInterface] > {} - def ASTCXXDestructDeclInterface : AttrInterface< "ASTCXXDestructDeclInterface", + def ASTCXXDestructorDeclInterface : AttrInterface< "ASTCXXDestructorDeclInterface", [ASTCXXMethodDeclInterface] > {} diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index f715b8f973c7..f1c45c9f7943 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -1804,8 +1804,20 @@ CIRGenModule::createCIRFunction(mlir::Location loc, StringRef name, f = builder.create(loc, name, Ty); - if (FD) - f.setAstAttr(builder.getAttr(FD)); + if (FD) { + auto makeAstAttr = [&] (auto decl) -> mlir::Attribute { + if (auto ast = clang::dyn_cast(decl)) + return builder.getAttr(ast); + if (auto ast = clang::dyn_cast(decl)) + return builder.getAttr(ast); + if (auto ast = clang::dyn_cast(decl)) + return builder.getAttr(ast); + if (auto ast = clang::dyn_cast(decl)) + return builder.getAttr(ast); + return builder.getAttr(decl); + }; + f.setAstAttr(makeAstAttr(FD)); + } if (FD && !FD->hasPrototype()) f.setNoProtoAttr(builder.getUnitAttr()); diff --git a/clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp b/clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp index 64f84024149a..392b7e0007ca 100644 --- a/clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp +++ b/clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp @@ -77,13 +77,13 @@ struct LifetimeCheckPass : public LifetimeCheckBase { void checkLambdaCaptureStore(StoreOp storeOp); void trackCallToCoroutine(CallOp callOp); - void checkCtor(CallOp callOp, const clang::CXXConstructorDecl *ctor); - void checkMoveAssignment(CallOp callOp, const clang::CXXMethodDecl *m); - void checkCopyAssignment(CallOp callOp, const clang::CXXMethodDecl *m); + void checkCtor(CallOp callOp, ASTCXXConstructorDeclInterface ctor); + void checkMoveAssignment(CallOp callOp, ASTCXXMethodDeclInterface m); + void checkCopyAssignment(CallOp callOp, ASTCXXMethodDeclInterface m); void checkNonConstUseOfOwner(mlir::Value ownerAddr, mlir::Location loc); - void checkOperators(CallOp callOp, const clang::CXXMethodDecl *m); + void checkOperators(CallOp callOp, ASTCXXMethodDeclInterface m); void checkOtherMethodsAndFunctions(CallOp callOp, - const clang::CXXMethodDecl *m); + ASTCXXMethodDeclInterface m); void checkForOwnerAndPointerArguments(CallOp callOp, unsigned firstArgIdx); // TODO: merge both methods below and pass down an enum. @@ -105,12 +105,11 @@ struct LifetimeCheckPass : public LifetimeCheckBase { std::optional currFunc; // Common helpers. - bool isCtorInitPointerFromOwner(CallOp callOp, - const clang::CXXConstructorDecl *ctor); + bool isCtorInitPointerFromOwner(CallOp callOp); mlir::Value getNonConstUseOfOwner(CallOp callOp, - const clang::CXXMethodDecl *m); + ASTCXXMethodDeclInterface m); bool isOwnerOrPointerClassMethod(CallOp callOp, - const clang::CXXMethodDecl *m); + ASTCXXMethodDeclInterface m); // Diagnostic helpers. void emitInvalidHistory(mlir::InFlightDiagnostic &D, mlir::Value histKey, @@ -1480,14 +1479,14 @@ static FuncOp getCalleeFromSymbol(ModuleOp mod, StringRef name) { return dyn_cast(global); } -static const clang::CXXMethodDecl *getMethod(ModuleOp mod, CallOp callOp) { +static const ASTCXXMethodDeclInterface getMethod(ModuleOp mod, CallOp callOp) { if (!callOp.getCallee()) return nullptr; StringRef name = *callOp.getCallee(); auto method = getCalleeFromSymbol(mod, name); if (!method || method.getBuiltin()) return nullptr; - return dyn_cast(method.getAstAttr().getAstDecl()); + return dyn_cast< ASTCXXMethodDeclInterface >(method.getAstAttr()); } mlir::Value LifetimeCheckPass::getThisParamPointerCategory(CallOp callOp) { @@ -1515,7 +1514,7 @@ mlir::Value LifetimeCheckPass::getThisParamOwnerCategory(CallOp callOp) { } void LifetimeCheckPass::checkMoveAssignment(CallOp callOp, - const clang::CXXMethodDecl *m) { + ASTCXXMethodDeclInterface m) { // MyPointer::operator=(MyPointer&&)(%dst, %src) // or // MyOwner::operator=(MyOwner&&)(%dst, %src) @@ -1548,7 +1547,7 @@ void LifetimeCheckPass::checkMoveAssignment(CallOp callOp, } void LifetimeCheckPass::checkCopyAssignment(CallOp callOp, - const clang::CXXMethodDecl *m) { + ASTCXXMethodDeclInterface m) { // MyIntOwner::operator=(MyIntOwner&)(%dst, %src) auto dst = getThisParamOwnerCategory(callOp); auto src = callOp.getArgOperand(1); @@ -1571,8 +1570,7 @@ void LifetimeCheckPass::checkCopyAssignment(CallOp callOp, // Example: // MyIntPointer::MyIntPointer(MyIntOwner const&)(%5, %4) // -bool LifetimeCheckPass::isCtorInitPointerFromOwner( - CallOp callOp, const clang::CXXConstructorDecl *ctor) { +bool LifetimeCheckPass::isCtorInitPointerFromOwner(CallOp callOp) { if (callOp.getNumArgOperands() < 2) return false; @@ -1587,7 +1585,7 @@ bool LifetimeCheckPass::isCtorInitPointerFromOwner( } void LifetimeCheckPass::checkCtor(CallOp callOp, - const clang::CXXConstructorDecl *ctor) { + ASTCXXConstructorDeclInterface ctor) { // TODO: zero init // 2.4.2 if the initialization is default initialization or zero // initialization, example: @@ -1596,7 +1594,7 @@ void LifetimeCheckPass::checkCtor(CallOp callOp, // string_view p; // // both results in pset(p) == {null} - if (ctor->isDefaultConstructor()) { + if (ctor.isDefaultConstructor()) { // First argument passed is always the alloca for the 'this' ptr. // Currently two possible actions: @@ -1620,11 +1618,11 @@ void LifetimeCheckPass::checkCtor(CallOp callOp, } // User defined copy ctor calls ... - if (ctor->isCopyConstructor()) { + if (ctor.isCopyConstructor()) { llvm_unreachable("NYI"); } - if (isCtorInitPointerFromOwner(callOp, ctor)) { + if (isCtorInitPointerFromOwner(callOp)) { auto addr = getThisParamPointerCategory(callOp); assert(addr && "expected pointer category"); auto owner = callOp.getArgOperand(1); @@ -1635,11 +1633,11 @@ void LifetimeCheckPass::checkCtor(CallOp callOp, } void LifetimeCheckPass::checkOperators(CallOp callOp, - const clang::CXXMethodDecl *m) { + ASTCXXMethodDeclInterface m) { auto addr = getThisParamOwnerCategory(callOp); if (addr) { // const access to the owner is fine. - if (m->isConst()) + if (m.isConst()) return; // TODO: this is a place where we can hook in some idiom recocgnition // so we don't need to use actual source code annotation to make assumptions @@ -1664,8 +1662,8 @@ void LifetimeCheckPass::checkOperators(CallOp callOp, mlir::Value LifetimeCheckPass::getNonConstUseOfOwner(CallOp callOp, - const clang::CXXMethodDecl *m) { - if (m->isConst()) + ASTCXXMethodDeclInterface m) { + if (m.isConst()) return {}; return getThisParamOwnerCategory(callOp); } @@ -1736,7 +1734,7 @@ void LifetimeCheckPass::checkForOwnerAndPointerArguments(CallOp callOp, } void LifetimeCheckPass::checkOtherMethodsAndFunctions( - CallOp callOp, const clang::CXXMethodDecl *m) { + CallOp callOp, ASTCXXMethodDeclInterface m) { unsigned firstArgIdx = 0; // Looks at a method 'this' pointer: @@ -1749,9 +1747,9 @@ void LifetimeCheckPass::checkOtherMethodsAndFunctions( } bool LifetimeCheckPass::isOwnerOrPointerClassMethod( - CallOp callOp, const clang::CXXMethodDecl *m) { + CallOp callOp, ASTCXXMethodDeclInterface m) { // For the sake of analysis, these behave like regular functions - if (!m || m->isStatic()) + if (!m || m.isStatic()) return false; // Check the object for owner/pointer by looking at the 'this' pointer. return getThisParamPointerCategory(callOp) || @@ -1835,13 +1833,13 @@ void LifetimeCheckPass::checkCall(CallOp callOp) { // From this point on only owner and pointer class methods handling, // starting from special methods. - if (auto ctor = dyn_cast(methodDecl)) + if (auto ctor = dyn_cast< ASTCXXConstructorDeclInterface >(methodDecl)) return checkCtor(callOp, ctor); - if (methodDecl->isMoveAssignmentOperator()) + if (methodDecl.isMoveAssignmentOperator()) return checkMoveAssignment(callOp, methodDecl); - if (methodDecl->isCopyAssignmentOperator()) + if (methodDecl.isCopyAssignmentOperator()) return checkCopyAssignment(callOp, methodDecl); - if (methodDecl->isOverloadedOperator()) + if (methodDecl.isOverloadedOperator()) return checkOperators(callOp, methodDecl); // For any other methods... From eea13904257faa24e4aeaab78859033017df7b74 Mon Sep 17 00:00:00 2001 From: Henrich Lauko Date: Sat, 2 Sep 2023 08:54:29 +0200 Subject: [PATCH 16/25] [CIR] Clean up decl interfaces. --- .../include/clang/CIR/Dialect/IR/CIRAttrs.td | 11 +-------- clang/include/clang/CIR/Dialect/IR/CIROps.td | 4 ++-- .../clang/CIR/Interfaces/ASTAttrInterfaces.h | 18 +++++++++++++-- .../clang/CIR/Interfaces/ASTAttrInterfaces.td | 23 ++++++++++++++----- clang/lib/CIR/Dialect/IR/CIRAttrs.cpp | 4 ++-- .../CIR/Dialect/Transforms/LifetimeCheck.cpp | 12 ++++++---- .../Dialect/Transforms/LoweringPrepare.cpp | 6 +++-- 7 files changed, 49 insertions(+), 29 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td index 800e5af16b58..0b89e3950d2d 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td @@ -398,16 +398,7 @@ class ASTDecl traits = []> // Enable verifier. let genVerifyDecl = 1; - let extraClassDeclaration = [{ - llvm::SmallVector< ASTCXXRecordDeclInterface, 4 > CXXRecordDecls() const; - }]; - let extraClassDefinition = [{ - llvm::SmallVector< ASTCXXRecordDeclInterface, 4 > - $cppClass::CXXRecordDecls() const { - return getCXXRecordDecls(getAstDecl(), getContext()); - } - ::mlir::Attribute $cppClass::parse(::mlir::AsmParser &parser, ::mlir::Type type) { // We cannot really parse anything AST related at this point @@ -463,7 +454,7 @@ def ASTTagDeclAttr : ASTDecl<"TagDecl", "tag.decl", [ASTTagDeclInterface] >; -def ASTenumDeclAttr : ASTDecl<"EnumDecl", "enum.decl", +def ASTEnumDeclAttr : ASTDecl<"EnumDecl", "enum.decl", [ASTEnumDeclInterface] >; diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index cb97f37b4dd8..5bac707a6107 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -295,7 +295,7 @@ def AllocaOp : CIR_Op<"alloca", [ StrAttr:$name, UnitAttr:$init, ConfinedAttr, [IntMinValue<0>]>:$alignment, - OptionalAttr:$ast + OptionalAttr:$ast ); let results = (outs Res:$initial_value, UnitAttr:$constant, OptionalAttr:$alignment, - OptionalAttr:$ast + OptionalAttr:$ast ); let regions = (region AnyRegion:$ctorRegion, AnyRegion:$dtorRegion); let assemblyFormat = [{ diff --git a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h index e8e380b0ab28..043d78dcd71e 100644 --- a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h +++ b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h @@ -15,10 +15,19 @@ #include "clang/AST/Mangle.h" #include "clang/AST/DeclTemplate.h" +namespace mlir { +namespace cir { + +mlir::Attribute makeAstDeclAttr(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::cir { +namespace mlir { +namespace cir { template< typename T > bool hasAttr(ASTDeclInterface decl) { @@ -29,8 +38,13 @@ namespace mlir::cir { 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 mlir::cir +} // namespace cir +} // namespace mlir #endif // MLIR_INTERFACES_CIR_AST_ATR_INTERFACES_H_ diff --git a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td index e8677ca803fb..c65ed36338c3 100644 --- a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td +++ b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td @@ -25,15 +25,29 @@ let cppNamespace = "::mlir::cir" in { return $_attr.getAstDecl()->template hasAttr< clang::PointerAttr >(); }] >, - InterfaceMethod< "", "ASTDeclInterface", "getNextDeclInContext", (ins), [{}], + InterfaceMethod< "", "bool", "hasInitPriorityAttr", (ins), [{}], /*defaultImplementation=*/ [{ - return $_attr.getNextDeclInContext(); + return $_attr.getAstDecl()->template hasAttr< clang::InitPriorityAttr >(); }] > ]; } - def ASTDeclContextInterface : AttrInterface< "ASTDeclContextInterface" > {} + def ASTDeclContextInterface : AttrInterface< "ASTDeclContextInterface" > { + let methods = [ + InterfaceMethod< "", + "llvm::SmallVector< ::mlir::Attribute, 4 >", "decls", + (ins "::mlir::MLIRContext *":$ctx), [{}], + /*defaultImplementation=*/ [{ + llvm::SmallVector< mlir::Attribute, 4 > decls; + for (const auto *sub : $_attr.getAstDecl()->decls()) { + decls.push_back(makeAstDeclAttr(sub, ctx)); + } + return decls; + }] + > + ]; + } def ASTNamedDeclInterface : AttrInterface< "ASTNamedDeclInterface", [ASTDeclInterface] @@ -166,9 +180,6 @@ let cppNamespace = "::mlir::cir" in { /*defaultImplementation=*/ [{ return $_attr.getAstDecl()->isLambda(); }] - >, - InterfaceMethod< "", - "llvm::SmallVector< ASTCXXRecordDeclInterface, 4 >", "CXXRecordDecls" > ]; } diff --git a/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp b/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp index 3133bbc3f8ec..d38fe5cb3b1a 100644 --- a/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp @@ -38,11 +38,11 @@ static mlir::ParseResult parseStructMembers(::mlir::AsmParser &parser, mlir::ArrayAttr &members); llvm::SmallVector< mlir::cir::ASTCXXRecordDeclInterface, 4 > -getCXXRecordDecls(const clang::DeclContext *ast, mlir::MLIRContext *ctx); +decls(const clang::DeclContext *ast, mlir::MLIRContext *ctx); template< typename DeclType > llvm::SmallVector< mlir::cir::ASTCXXRecordDeclInterface, 4 > -getCXXRecordDecls(const DeclType *ast, mlir::MLIRContext *ctx); +decls(const DeclType *ast, mlir::MLIRContext *ctx); #define GET_ATTRDEF_CLASSES #include "clang/CIR/Dialect/IR/CIROpsAttributes.cpp.inc" diff --git a/clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp b/clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp index 392b7e0007ca..57bca6060dff 100644 --- a/clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp +++ b/clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp @@ -1781,15 +1781,17 @@ bool LifetimeCheckPass::isTaskType(mlir::Value taskVal) { if (!taskTy) return false; auto recordDecl = taskTy.getAst(); - // FIXME we are not emmiting ASTClassTemplateSpecializationDeclInterface anywhere auto spec = dyn_cast< ASTClassTemplateSpecializationDeclInterface >(recordDecl); if (!spec) return false; - for (auto subRec : spec.CXXRecordDecls()) { - if (subRec.getDeclName().isIdentifier() && subRec.getName() == "promise_type") { - IsTaskTyCache[ty] = true; - break; + for (auto sub : spec.decls(taskVal.getContext())) { + if (auto subRec = dyn_cast< ASTCXXRecordDeclInterface >(sub)) { + if (subRec.getDeclName().isIdentifier() && + subRec.getName() == "promise_type") { + IsTaskTyCache[ty] = true; + break; + } } } diff --git a/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp b/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp index c5a2ca694d9b..3640594930d0 100644 --- a/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp +++ b/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp @@ -14,6 +14,7 @@ #include "clang/AST/Mangle.h" #include "clang/Basic/Module.h" #include "clang/CIR/Dialect/IR/CIRDialect.h" +#include "clang/CIR/Interfaces/ASTAttrInterfaces.h" #include "clang/CIR/Dialect/Passes.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringMap.h" @@ -136,7 +137,7 @@ cir::FuncOp LoweringPreparePass::buildCXXGlobalVarDeclInitFunc(GlobalOp op) { SmallString<256> fnName; { llvm::raw_svector_ostream Out(fnName); - op.getAst()->mangleDynamicInitializer(Out); + mlir::cast< ASTVarDeclInterface >(*op.getAst()).mangleDynamicInitializer(Out); // Name numbering uint32_t cnt = dynamicInitializerNames[fnName]++; if (cnt) @@ -238,7 +239,8 @@ void LoweringPreparePass::lowerGlobalOp(GlobalOp op) { dtorRegion.getBlocks().clear(); // Add a function call to the variable initialization function. - assert(!op.getAst()->getAstDecl()->getAttr() && + assert(!hasAttr( + mlir::cast(*op.getAst())) && "custom initialization priority NYI"); dynamicInitializers.push_back(f); } From 41ee67188f23cd7a9169e328664be0726e793e4c Mon Sep 17 00:00:00 2001 From: Henrich Lauko Date: Sat, 2 Sep 2023 08:55:16 +0200 Subject: [PATCH 17/25] [CIR] Unify AST attr construction. --- clang/lib/CIR/CodeGen/CIRGenCXX.cpp | 1 + clang/lib/CIR/CodeGen/CIRGenExpr.cpp | 2 +- clang/lib/CIR/CodeGen/CIRGenModule.cpp | 13 +---- clang/lib/CIR/CodeGen/CIRGenTypes.cpp | 3 +- .../CIR/CodeGen/CIRRecordLayoutBuilder.cpp | 6 +-- clang/lib/CIR/Dialect/IR/CIRAttrs.cpp | 51 +++++++++++-------- 6 files changed, 37 insertions(+), 39 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenCXX.cpp b/clang/lib/CIR/CodeGen/CIRGenCXX.cpp index d6c33dcd5ce7..0d19fe02c531 100644 --- a/clang/lib/CIR/CodeGen/CIRGenCXX.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenCXX.cpp @@ -121,6 +121,7 @@ void CIRGenModule::codegenGlobalInitCxxStructor(const VarDecl *D, buildDeclInit(CGF, D, DeclAddr); builder.setInsertionPointToEnd(block); builder.create(Addr->getLoc()); + Addr.setAstAttr(mlir::cir::makeAstDeclAttr(D, builder.getContext())); } if (NeedsDtor) { diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp index 749f32292e25..b265bc4ae850 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp @@ -2188,7 +2188,7 @@ mlir::Value CIRGenFunction::buildAlloca(StringRef name, mlir::Type ty, alignIntAttr); if (currVarDecl) { auto alloca = cast(addr.getDefiningOp()); - alloca.setAstAttr(ASTVarDeclAttr::get(builder.getContext(), currVarDecl)); + alloca.setAstAttr(mlir::cir::makeAstDeclAttr(currVarDecl, builder.getContext())); } } return addr; diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index f1c45c9f7943..5b53f4026ac8 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -1805,18 +1805,7 @@ CIRGenModule::createCIRFunction(mlir::Location loc, StringRef name, f = builder.create(loc, name, Ty); if (FD) { - auto makeAstAttr = [&] (auto decl) -> mlir::Attribute { - if (auto ast = clang::dyn_cast(decl)) - return builder.getAttr(ast); - if (auto ast = clang::dyn_cast(decl)) - return builder.getAttr(ast); - if (auto ast = clang::dyn_cast(decl)) - return builder.getAttr(ast); - if (auto ast = clang::dyn_cast(decl)) - return builder.getAttr(ast); - return builder.getAttr(decl); - }; - f.setAstAttr(makeAstAttr(FD)); + f.setAstAttr(makeAstDeclAttr(FD, builder.getContext())); } if (FD && !FD->hasPrototype()) diff --git a/clang/lib/CIR/CodeGen/CIRGenTypes.cpp b/clang/lib/CIR/CodeGen/CIRGenTypes.cpp index 3ad25f3fbc76..2d1fd912f367 100644 --- a/clang/lib/CIR/CodeGen/CIRGenTypes.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenTypes.cpp @@ -167,7 +167,8 @@ mlir::Type CIRGenTypes::convertRecordDeclType(const clang::RecordDecl *RD) { // Handle forward decl / incomplete types. if (!entry) { auto name = getRecordTypeName(RD, ""); - entry = Builder.getStructTy({}, name, /*body=*/false, /*packed=*/false, RD); + entry = Builder.getStructTy({}, name, /*body=*/false, /*packed=*/false, + mlir::cir::makeAstDeclAttr(RD, &getMLIRContext())); recordDeclTypes[key] = entry; } diff --git a/clang/lib/CIR/CodeGen/CIRRecordLayoutBuilder.cpp b/clang/lib/CIR/CodeGen/CIRRecordLayoutBuilder.cpp index ec80cbf50c01..a3f96d08fd9e 100644 --- a/clang/lib/CIR/CodeGen/CIRRecordLayoutBuilder.cpp +++ b/clang/lib/CIR/CodeGen/CIRRecordLayoutBuilder.cpp @@ -607,8 +607,7 @@ CIRGenTypes::computeRecordLayout(const RecordDecl *D, auto baseIdentifier = getRecordTypeName(D, ".base"); *BaseTy = Builder.getStructTy(baseBuilder.fieldTypes, baseIdentifier, /*body=*/true, /*packed=*/false, - mlir::cir::ASTCXXRecordDeclAttr::get(&getMLIRContext(), - llvm::dyn_cast(D))); + mlir::cir::makeAstDeclAttr(D, &getMLIRContext())); // TODO(cir): add something like addRecordTypeName // BaseTy and Ty must agree on their packedness for getCIRFieldNo to work @@ -622,7 +621,8 @@ CIRGenTypes::computeRecordLayout(const RecordDecl *D, // signifies that the type is no longer opaque and record layout is complete, // but we may need to recursively layout D while laying D out as a base type. *Ty = Builder.getStructTy(builder.fieldTypes, getRecordTypeName(D, ""), - /*body=*/true, /*packed=*/false, D); + /*body=*/true, /*packed=*/false, + mlir::cir::makeAstDeclAttr(D, &getMLIRContext())); auto RL = std::make_unique( Ty ? *Ty : mlir::cir::StructType{}, diff --git a/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp b/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp index d38fe5cb3b1a..c2ba79f6d98b 100644 --- a/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp @@ -54,28 +54,35 @@ using namespace mlir::cir; // CIR AST Attr helpers //===----------------------------------------------------------------------===// -llvm::SmallVector< ASTCXXRecordDeclInterface, 4 > -getCXXRecordDecls(const clang::DeclContext *ast, MLIRContext *ctx) { - llvm::SmallVector< ASTCXXRecordDeclInterface, 4 > records; - - for (auto sub : ast->decls()) { - if (auto rec = clang::dyn_cast< clang::CXXRecordDecl >(sub)) { - records.emplace_back( - ASTCXXRecordDeclAttr::get(ctx, rec) - ); - } - } - - return records; -} - -template< typename DeclType > -llvm::SmallVector< ASTCXXRecordDeclInterface, 4 > -getCXXRecordDecls(const DeclType *ast, MLIRContext *ctx) { - if (auto decl = llvm::dyn_cast< clang::DeclContext >(ast)) - return getCXXRecordDecls(decl, ctx); - return {}; -} +mlir::Attribute mlir::cir::makeAstDeclAttr(const clang::Decl *decl, mlir::MLIRContext *ctx) { + if (auto ast = clang::dyn_cast(decl)) + return ASTCXXConstructorDeclAttr::get(ctx, ast); + if (auto ast = clang::dyn_cast(decl)) + return ASTCXXConversionDeclAttr::get(ctx, ast); + if (auto ast = clang::dyn_cast(decl)) + return ASTCXXDestructorDeclAttr::get(ctx, ast); + if (auto ast = clang::dyn_cast(decl)) + return ASTCXXMethodDeclAttr::get(ctx, ast); + if (auto ast = clang::dyn_cast(decl)) + return ASTFunctionDeclAttr::get(ctx, ast); + if (auto ast = clang::dyn_cast(decl)) + return ASTClassTemplatePartialSpecializationDeclAttr::get(ctx, ast); + if (auto ast = clang::dyn_cast(decl)) + return ASTClassTemplateSpecializationDeclAttr::get(ctx, ast); + if (auto ast = clang::dyn_cast(decl)) + return ASTCXXRecordDeclAttr::get(ctx, ast); + if (auto ast = clang::dyn_cast(decl)) + return ASTRecordDeclAttr::get(ctx, ast); + if (auto ast = clang::dyn_cast(decl)) + return ASTEnumDeclAttr::get(ctx, ast); + if (auto ast = clang::dyn_cast(decl)) + return ASTTagDeclAttr::get(ctx, ast); + if (auto ast = clang::dyn_cast(decl)) + return ASTTypeDeclAttr::get(ctx, ast); + if (auto ast = clang::dyn_cast(decl)) + return ASTVarDeclAttr::get(ctx, ast); + return ASTDeclAttr::get(ctx, decl); +}; //===----------------------------------------------------------------------===// // General CIR parsing / printing From ac519c1cec3a79610c4a43eedd4046596d9d78c9 Mon Sep 17 00:00:00 2001 From: Henrich Lauko Date: Sat, 2 Sep 2023 14:00:21 +0200 Subject: [PATCH 18/25] [CIR] Add TLS and Tag kind to AST Interface. --- .../clang/CIR/Interfaces/ASTAttrInterfaces.td | 15 ++++++++++++- clang/lib/CIR/CodeGen/CIRGenBuilder.h | 22 ++++++++++++++----- clang/lib/CIR/CodeGen/CIRGenCXX.cpp | 3 +-- .../Dialect/Transforms/LoweringPrepare.cpp | 6 +++-- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td index c65ed36338c3..5c90491053f3 100644 --- a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td +++ b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td @@ -84,6 +84,11 @@ let cppNamespace = "::mlir::cir" in { $_attr.getAstDecl()->getASTContext().createMangleContext()); MangleCtx->mangleDynamicInitializer($_attr.getAstDecl(), Out); }] + >, + InterfaceMethod< "", "clang::VarDecl::TLSKind", "getTLSKind", (ins), [{}], + /*defaultImplementation=*/ [{ + return $_attr.getAstDecl()->getTLSKind(); + }] > ]; } @@ -162,7 +167,15 @@ let cppNamespace = "::mlir::cir" in { def ASTTagDeclInterface : AttrInterface< "ASTTagDeclInterface", [ASTTypeDeclInterface, ASTDeclContextInterface] - > {} + > { + let methods = [ + InterfaceMethod< "", "clang::TagTypeKind", "getTagKind", (ins), [{}], + /*defaultImplementation=*/ [{ + return $_attr.getAstDecl()->getTagKind(); + }] + > + ]; + } def ASTEnumDeclInterface : AttrInterface< "ASTEnumDeclInterface", [ASTTagDeclInterface] diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.h b/clang/lib/CIR/CodeGen/CIRGenBuilder.h index 68bc26a6518d..49f253ab05cc 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuilder.h +++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.h @@ -170,7 +170,7 @@ class CIRGenBuilderTy : public mlir::OpBuilder { if (!structTy) structTy = getType( members, mlir::StringAttr::get(getContext()), - /*body=*/true, packed, mlir::cir::StructType::Struct + /*body=*/true, packed, mlir::cir::StructType::Struct, /*ast=*/mlir::Attribute()); // Return zero or anonymous constant struct. @@ -413,18 +413,28 @@ class CIRGenBuilderTy : public mlir::OpBuilder { /// Get a CIR named struct type. mlir::cir::StructType getStructTy(llvm::ArrayRef members, llvm::StringRef name, bool body, - bool packed, const clang::RecordDecl *ast) { + bool packed, mlir::Attribute astAttr) { const auto nameAttr = getStringAttr(name); - std::optional astAttr = std::nullopt; auto kind = mlir::cir::StructType::RecordKind::Struct; - if (ast) { - astAttr = getAttr(ast); - kind = getRecordKind(ast->getTagKind()); + if (astAttr) { + if (auto tagDecl = mlir::dyn_cast< mlir::cir::ASTTagDeclInterface >(astAttr)) { + kind = getRecordKind(tagDecl.getTagKind()); + } } return mlir::cir::StructType::get(getContext(), members, nameAttr, body, packed, kind, astAttr); } + mlir::cir::StructType getStructTy(llvm::ArrayRef members, + llvm::StringRef name, bool body, + bool packed, const clang::RecordDecl *ast) { + mlir::Attribute astAttr; + if (ast) { + astAttr = mlir::cir::makeAstDeclAttr(ast, getContext()); + } + return getStructTy(members, name, body, packed, astAttr); + } + // // Constant creation helpers // ------------------------- diff --git a/clang/lib/CIR/CodeGen/CIRGenCXX.cpp b/clang/lib/CIR/CodeGen/CIRGenCXX.cpp index 0d19fe02c531..33d59d4d7207 100644 --- a/clang/lib/CIR/CodeGen/CIRGenCXX.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenCXX.cpp @@ -111,7 +111,7 @@ void CIRGenModule::codegenGlobalInitCxxStructor(const VarDecl *D, CIRGenFunction CGF{*this, builder, true}; CurCGF = &CGF; CurCGF->CurFn = Addr; - Addr.setAstAttr(mlir::cir::ASTVarDeclAttr::get(builder.getContext(), D)); + Addr.setAstAttr(mlir::cir::makeAstDeclAttr(D, builder.getContext())); if (NeedsCtor) { mlir::OpBuilder::InsertionGuard guard(builder); @@ -121,7 +121,6 @@ void CIRGenModule::codegenGlobalInitCxxStructor(const VarDecl *D, buildDeclInit(CGF, D, DeclAddr); builder.setInsertionPointToEnd(block); builder.create(Addr->getLoc()); - Addr.setAstAttr(mlir::cir::makeAstDeclAttr(D, builder.getContext())); } if (NeedsDtor) { diff --git a/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp b/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp index 3640594930d0..5994710aadc3 100644 --- a/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp +++ b/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp @@ -162,7 +162,9 @@ cir::FuncOp LoweringPreparePass::buildCXXGlobalVarDeclInitFunc(GlobalOp op) { // Register the destructor call with __cxa_atexit - assert(io.getAst()->getTLSKind() == clang::VarDecl::TLS_None && " TLS NYI"); + assert(mlir::isa< ASTVarDeclInterface >(*op.getAst()) && + mlir::dyn_cast< ASTVarDeclInterface >(*op.getAst()).getTLSKind() == + clang::VarDecl::TLS_None && " TLS NYI"); // Create a variable that binds the atexit to this shared object. builder.setInsertionPointToStart(&theModule.getBodyRegion().front()); auto Handle = buildRuntimeVariable(builder, "__dso_handle", op.getLoc(), @@ -178,7 +180,7 @@ cir::FuncOp LoweringPreparePass::buildCXXGlobalVarDeclInitFunc(GlobalOp op) { assert(dtorCall && "Expected a dtor call"); cir::FuncOp dtorFunc = getCalledFunction(dtorCall); assert(dtorFunc && - isa(dtorFunc.getAst()->getAstDecl()) && + mlir::isa< ASTCXXDestructorDeclInterface >(*dtorFunc.getAst()) && "Expected a dtor call"); // Create a runtime helper function: From dcecbedaebec69ea6963eda19d214b38dddcfbaa Mon Sep 17 00:00:00 2001 From: Henrich Lauko Date: Thu, 7 Sep 2023 09:36:15 +0200 Subject: [PATCH 19/25] [CIR] Apply formatting to attr interface changes. --- .../include/clang/CIR/Dialect/IR/CIRAttrs.td | 66 ++- .../clang/CIR/Interfaces/ASTAttrInterfaces.h | 33 +- .../clang/CIR/Interfaces/ASTAttrInterfaces.td | 375 +++++++++--------- clang/lib/CIR/CodeGen/CIRGenBuilder.h | 13 +- clang/lib/CIR/CodeGen/CIRGenExpr.cpp | 7 +- clang/lib/CIR/CodeGen/CIRGenModule.cpp | 17 +- clang/lib/CIR/CodeGen/CIRGenTypes.cpp | 5 +- .../CIR/CodeGen/CIRRecordLayoutBuilder.cpp | 7 +- clang/lib/CIR/Dialect/IR/CIRAttrs.cpp | 73 ++-- .../CIR/Dialect/Transforms/LifetimeCheck.cpp | 18 +- .../Dialect/Transforms/LoweringPrepare.cpp | 31 +- 11 files changed, 302 insertions(+), 343 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td index 0b89e3950d2d..212638a0f434 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td @@ -400,7 +400,7 @@ class ASTDecl traits = []> let extraClassDefinition = [{ ::mlir::Attribute $cppClass::parse(::mlir::AsmParser &parser, - ::mlir::Type type) { + ::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); @@ -418,65 +418,49 @@ class ASTDecl traits = []> }]; } -def ASTDeclAttr : ASTDecl<"Decl", "decl", - [ASTDeclInterface] ->; +def ASTDeclAttr : ASTDecl<"Decl", "decl", [ASTDeclInterface]>; def ASTFunctionDeclAttr : ASTDecl<"FunctionDecl", "function.decl", - [ASTFunctionDeclInterface] ->; + [ASTFunctionDeclInterface]>; def ASTCXXMethodDeclAttr : ASTDecl<"CXXMethodDecl", "cxxmethod.decl", - [ASTCXXMethodDeclInterface] ->; + [ASTCXXMethodDeclInterface]>; -def ASTCXXConstructorDeclAttr : ASTDecl<"CXXConstructorDecl", "cxxconstructor.decl", - [ASTCXXConstructorDeclInterface] ->; +def ASTCXXConstructorDeclAttr : ASTDecl<"CXXConstructorDecl", + "cxxconstructor.decl", [ASTCXXConstructorDeclInterface]>; -def ASTCXXConversionDeclAttr : ASTDecl<"CXXConversionDecl", "cxxconversion.decl", - [ASTCXXConversionDeclInterface] ->; +def ASTCXXConversionDeclAttr : ASTDecl<"CXXConversionDecl", + "cxxconversion.decl", [ASTCXXConversionDeclInterface]>; -def ASTCXXDestructorDeclAttr : ASTDecl<"CXXDestructorDecl", "cxxdestructor.decl", - [ASTCXXDestructorDeclInterface] ->; +def ASTCXXDestructorDeclAttr : ASTDecl<"CXXDestructorDecl", + "cxxdestructor.decl", [ASTCXXDestructorDeclInterface]>; def ASTVarDeclAttr : ASTDecl<"VarDecl", "var.decl", - [ASTVarDeclInterface] ->; + [ASTVarDeclInterface]>; def ASTTypeDeclAttr: ASTDecl<"TypeDecl", "type.decl", - [ASTTypeDeclInterface] ->; + [ASTTypeDeclInterface]>; def ASTTagDeclAttr : ASTDecl<"TagDecl", "tag.decl", - [ASTTagDeclInterface] ->; + [ASTTagDeclInterface]>; def ASTEnumDeclAttr : ASTDecl<"EnumDecl", "enum.decl", - [ASTEnumDeclInterface] ->; + [ASTEnumDeclInterface]>; def ASTRecordDeclAttr : ASTDecl<"RecordDecl", "record.decl", - [ASTRecordDeclInterface] ->; + [ASTRecordDeclInterface]>; def ASTCXXRecordDeclAttr : ASTDecl<"CXXRecordDecl", "cxxrecord.decl", - [ASTCXXRecordDeclInterface] ->; - -def ASTClassTemplateSpecializationDeclAttr - : ASTDecl<"ClassTemplateSpecializationDecl" - , "class.template.spec.decl" - , [ASTClassTemplateSpecializationDeclInterface] ->; - -def ASTClassTemplatePartialSpecializationDeclAttr - : ASTDecl<"ClassTemplatePartialSpecializationDecl" - , "class.template.partial.spec.decl" - , [ASTClassTemplatePartialSpecializationDeclInterface] ->; + [ASTCXXRecordDeclInterface]>; + +def ASTClassTemplateSpecializationDeclAttr : + ASTDecl<"ClassTemplateSpecializationDecl", "class.template.spec.decl", + [ASTClassTemplateSpecializationDeclInterface]>; + +def ASTClassTemplatePartialSpecializationDeclAttr : + ASTDecl<"ClassTemplatePartialSpecializationDecl", + "class.template.partial.spec.decl", + [ASTClassTemplatePartialSpecializationDeclInterface]>; //===----------------------------------------------------------------------===// // ExtraFuncAttr diff --git a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h index 043d78dcd71e..06fd329e8725 100644 --- a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h +++ b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h @@ -6,19 +6,20 @@ // //===----------------------------------------------------------------------===// -#ifndef MLIR_INTERFACES_CIR_AST_ATR_INTERFACES_H_ -#define MLIR_INTERFACES_CIR_AST_ATR_INTERFACES_H_ +#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/Mangle.h" #include "clang/AST/DeclTemplate.h" +#include "clang/AST/Mangle.h" namespace mlir { namespace cir { -mlir::Attribute makeAstDeclAttr(const clang::Decl *decl, mlir::MLIRContext *ctx); +mlir::Attribute makeAstDeclAttr(const clang::Decl *decl, + mlir::MLIRContext *ctx); } // namespace cir } // namespace mlir @@ -29,22 +30,16 @@ mlir::Attribute makeAstDeclAttr(const clang::Decl *decl, mlir::MLIRContext *ctx) 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(); - } - } +template bool hasAttr(ASTDeclInterface decl) { + if constexpr (std::is_same_v) + return decl.hasOwnerAttr(); + if constexpr (std::is_same_v) + return decl.hasPointerAttr(); + if constexpr (std::is_same_v) + return decl.hasInitPriorityAttr(); +} } // namespace cir } // namespace mlir -#endif // MLIR_INTERFACES_CIR_AST_ATR_INTERFACES_H_ +#endif // MLIR_INTERFACES_CIR_AST_ATAR_INTERFACES_H_ diff --git a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td index 5c90491053f3..1e4dc3ed190d 100644 --- a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td +++ b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td @@ -12,202 +12,183 @@ include "mlir/IR/OpBase.td" let cppNamespace = "::mlir::cir" in { - - def ASTDeclInterface : AttrInterface< "ASTDeclInterface" > { - let methods = [ - InterfaceMethod< "", "bool", "hasOwnerAttr", (ins), [{}], - /*defaultImplementation=*/ [{ - return $_attr.getAstDecl()->template hasAttr< clang::OwnerAttr >(); - }] - >, - InterfaceMethod< "", "bool", "hasPointerAttr", (ins), [{}], - /*defaultImplementation=*/ [{ - return $_attr.getAstDecl()->template hasAttr< clang::PointerAttr >(); - }] - >, - InterfaceMethod< "", "bool", "hasInitPriorityAttr", (ins), [{}], - /*defaultImplementation=*/ [{ - return $_attr.getAstDecl()->template hasAttr< clang::InitPriorityAttr >(); - }] - > - ]; - } - - def ASTDeclContextInterface : AttrInterface< "ASTDeclContextInterface" > { - let methods = [ - InterfaceMethod< "", - "llvm::SmallVector< ::mlir::Attribute, 4 >", "decls", - (ins "::mlir::MLIRContext *":$ctx), [{}], - /*defaultImplementation=*/ [{ - llvm::SmallVector< mlir::Attribute, 4 > decls; - for (const auto *sub : $_attr.getAstDecl()->decls()) { - decls.push_back(makeAstDeclAttr(sub, ctx)); - } - return decls; - }] - > - ]; - } - - def ASTNamedDeclInterface : AttrInterface< "ASTNamedDeclInterface", - [ASTDeclInterface] - > { - let methods = [ - InterfaceMethod< "", "clang::DeclarationName", "getDeclName", (ins), [{}], - /*defaultImplementation=*/ [{ - return $_attr.getAstDecl()->getDeclName(); - }] - >, - InterfaceMethod< "", "llvm::StringRef", "getName", (ins), [{}], - /*defaultImplementation=*/ [{ - return $_attr.getAstDecl()->getName(); - }] - > - ]; - } - - def ASTValueDeclInterface : AttrInterface< "ASTValueDeclInterface", - [ASTNamedDeclInterface] - > {} - - def ASTDeclaratorDeclInterface : AttrInterface< "ASTDeclaratorDeclInterface", - [ASTValueDeclInterface] - > {} - - def ASTVarDeclInterface : AttrInterface< "ASTVarDeclInterface", - [ASTDeclaratorDeclInterface] - > { - let methods = [ - InterfaceMethod< "", "void", "mangleDynamicInitializer", (ins "llvm::raw_ostream&":$Out), [{}], - /*defaultImplementation=*/ [{ - std::unique_ptr MangleCtx( - $_attr.getAstDecl()->getASTContext().createMangleContext()); - MangleCtx->mangleDynamicInitializer($_attr.getAstDecl(), Out); - }] - >, - InterfaceMethod< "", "clang::VarDecl::TLSKind", "getTLSKind", (ins), [{}], - /*defaultImplementation=*/ [{ - return $_attr.getAstDecl()->getTLSKind(); - }] - > - ]; - } - - def ASTFunctionDeclInterface : AttrInterface< "ASTFunctionDeclInterface", - [ASTDeclaratorDeclInterface, ASTDeclContextInterface] - > { - let methods = [ - InterfaceMethod< "", "bool", "isOverloadedOperator", (ins), [{}], - /*defaultImplementation=*/ [{ - return $_attr.getAstDecl()->isOverloadedOperator(); - }] - >, - InterfaceMethod< "", "bool", "isStatic", (ins), [{}], - /*defaultImplementation=*/ [{ - return $_attr.getAstDecl()->isStatic(); - }] - > - ]; - } - - def ASTCXXMethodDeclInterface : AttrInterface< "ASTCXXMethodDeclInterface", - [ASTFunctionDeclInterface] - > { - let methods = [ - InterfaceMethod< "", "bool", "isCopyAssignmentOperator", (ins), [{}], - /*defaultImplementation=*/ [{ - if (auto decl = dyn_cast< clang::CXXMethodDecl >($_attr.getAstDecl())) - return decl->isCopyAssignmentOperator(); - return false; - }] - >, - InterfaceMethod< "", "bool", "isMoveAssignmentOperator", (ins), [{}], - /*defaultImplementation=*/ [{ - if (auto decl = dyn_cast< clang::CXXMethodDecl >($_attr.getAstDecl())) - return decl->isMoveAssignmentOperator(); - return false; - }] - >, - InterfaceMethod< "", "bool", "isConst", (ins), [{}], - /*defaultImplementation=*/ [{ - return $_attr.getAstDecl()->isConst(); - }] - > - ]; - } - - def ASTCXXConstructorDeclInterface : AttrInterface< "ASTCXXConstructorDeclInterface", - [ASTCXXMethodDeclInterface] - > { - let methods = [ - InterfaceMethod< "", "bool", "isDefaultConstructor", (ins), [{}], - /*defaultImplementation=*/ [{ - return $_attr.getAstDecl()->isDefaultConstructor(); - }] - >, - InterfaceMethod< "", "bool", "isCopyConstructor", (ins), [{}], - /*defaultImplementation=*/ [{ - return $_attr.getAstDecl()->isCopyConstructor(); - }] - > - ]; - } - - def ASTCXXConversionDeclInterface : AttrInterface< "ASTCXXConversionDeclInterface", - [ASTCXXMethodDeclInterface] - > {} - - def ASTCXXDestructorDeclInterface : AttrInterface< "ASTCXXDestructorDeclInterface", - [ASTCXXMethodDeclInterface] - > {} - - def ASTTypeDeclInterface : AttrInterface< "ASTTypeDeclInterface", - [ASTNamedDeclInterface] - > {} - - def ASTTagDeclInterface : AttrInterface< "ASTTagDeclInterface", - [ASTTypeDeclInterface, ASTDeclContextInterface] - > { - let methods = [ - InterfaceMethod< "", "clang::TagTypeKind", "getTagKind", (ins), [{}], - /*defaultImplementation=*/ [{ - return $_attr.getAstDecl()->getTagKind(); - }] - > - ]; - } - - def ASTEnumDeclInterface : AttrInterface< "ASTEnumDeclInterface", - [ASTTagDeclInterface] - > {} - - def ASTRecordDeclInterface : AttrInterface< "ASTRecordDeclInterface", - [ASTTagDeclInterface] - > {} - - def ASTCXXRecordDeclInterface : AttrInterface< "ASTCXXRecordDeclInterface", - [ASTRecordDeclInterface] - > { - let methods = [ - InterfaceMethod< "", "bool", "isLambda", (ins), [{}], - /*defaultImplementation=*/ [{ - return $_attr.getAstDecl()->isLambda(); - }] - > - ]; - } - - def ASTClassTemplateSpecializationDeclInterface - : AttrInterface< "ASTClassTemplateSpecializationDeclInterface", - [ASTCXXRecordDeclInterface] - > - {} - - def ASTClassTemplatePartialSpecializationDeclInterface - : AttrInterface< "ASTClassTemplatePartialSpecializationDeclInterface", - [ASTClassTemplateSpecializationDeclInterface] - > - {} -} + def ASTDeclInterface : AttrInterface<"ASTDeclInterface"> { + let methods = [ + InterfaceMethod<"", "bool", "hasOwnerAttr", (ins), [{}], + /*defaultImplementation=*/ [{ + return $_attr.getAstDecl()->template hasAttr< clang::OwnerAttr >(); + }] + >, + InterfaceMethod<"", "bool", "hasPointerAttr", (ins), [{}], + /*defaultImplementation=*/ [{ + return $_attr.getAstDecl()->template hasAttr< clang::PointerAttr >(); + }] + >, + InterfaceMethod<"", "bool", "hasInitPriorityAttr", (ins), [{}], + /*defaultImplementation=*/ [{ + return $_attr.getAstDecl()->template hasAttr< clang::InitPriorityAttr >(); + }] + > + ]; + } + + def ASTDeclContextInterface : AttrInterface<"ASTDeclContextInterface"> { + let methods = [ + InterfaceMethod<"", "llvm::SmallVector< ::mlir::Attribute, 4 >", "decls", + (ins "::mlir::MLIRContext *":$ctx), [{}], + /*defaultImplementation=*/ [{ + llvm::SmallVector< mlir::Attribute, 4 > decls; + for (const auto *sub : $_attr.getAstDecl()->decls()) { + decls.push_back(makeAstDeclAttr(sub, ctx)); + } + return decls; + }] + > + ]; + } + + def ASTNamedDeclInterface : AttrInterface<"ASTNamedDeclInterface", + [ASTDeclInterface]> { + let methods = [ + InterfaceMethod<"", "clang::DeclarationName", "getDeclName", (ins), [{}], + /*defaultImplementation=*/ [{ + return $_attr.getAstDecl()->getDeclName(); + }] + >, + InterfaceMethod<"", "llvm::StringRef", "getName", (ins), [{}], + /*defaultImplementation=*/ [{ + return $_attr.getAstDecl()->getName(); + }] + > + ]; + } + + def ASTValueDeclInterface : AttrInterface<"ASTValueDeclInterface", + [ASTNamedDeclInterface]>; + + def ASTDeclaratorDeclInterface : AttrInterface<"ASTDeclaratorDeclInterface", + [ASTValueDeclInterface]>; + + def ASTVarDeclInterface : AttrInterface<"ASTVarDeclInterface", + [ASTDeclaratorDeclInterface]> { + let methods = [ + InterfaceMethod<"", "void", "mangleDynamicInitializer", (ins "llvm::raw_ostream&":$Out), [{}], + /*defaultImplementation=*/ [{ + std::unique_ptr MangleCtx( + $_attr.getAstDecl()->getASTContext().createMangleContext()); + MangleCtx->mangleDynamicInitializer($_attr.getAstDecl(), Out); + }] + >, + InterfaceMethod<"", "clang::VarDecl::TLSKind", "getTLSKind", (ins), [{}], + /*defaultImplementation=*/ [{ + return $_attr.getAstDecl()->getTLSKind(); + }] + > + ]; + } + + def ASTFunctionDeclInterface : AttrInterface<"ASTFunctionDeclInterface", + [ASTDeclaratorDeclInterface, ASTDeclContextInterface]> { + let methods = [ + InterfaceMethod<"", "bool", "isOverloadedOperator", (ins), [{}], + /*defaultImplementation=*/ [{ + return $_attr.getAstDecl()->isOverloadedOperator(); + }] + >, + InterfaceMethod<"", "bool", "isStatic", (ins), [{}], + /*defaultImplementation=*/ [{ + return $_attr.getAstDecl()->isStatic(); + }] + > + ]; + } + + def ASTCXXMethodDeclInterface : AttrInterface<"ASTCXXMethodDeclInterface", + [ASTFunctionDeclInterface]> { + let methods = [ + InterfaceMethod<"", "bool", "isCopyAssignmentOperator", (ins), [{}], + /*defaultImplementation=*/ [{ + if (auto decl = dyn_cast< clang::CXXMethodDecl >($_attr.getAstDecl())) + return decl->isCopyAssignmentOperator(); + return false; + }] + >, + InterfaceMethod<"", "bool", "isMoveAssignmentOperator", (ins), [{}], + /*defaultImplementation=*/ [{ + if (auto decl = dyn_cast< clang::CXXMethodDecl >($_attr.getAstDecl())) + return decl->isMoveAssignmentOperator(); + return false; + }] + >, + InterfaceMethod<"", "bool", "isConst", (ins), [{}], + /*defaultImplementation=*/ [{ + return $_attr.getAstDecl()->isConst(); + }] + > + ]; + } + + def ASTCXXConstructorDeclInterface : AttrInterface<"ASTCXXConstructorDeclInterface", + [ASTCXXMethodDeclInterface]> { + let methods = [ + InterfaceMethod<"", "bool", "isDefaultConstructor", (ins), [{}], + /*defaultImplementation=*/ [{ + return $_attr.getAstDecl()->isDefaultConstructor(); + }] + >, + InterfaceMethod<"", "bool", "isCopyConstructor", (ins), [{}], + /*defaultImplementation=*/ [{ + return $_attr.getAstDecl()->isCopyConstructor(); + }] + > + ]; + } + + def ASTCXXConversionDeclInterface : AttrInterface<"ASTCXXConversionDeclInterface", + [ASTCXXMethodDeclInterface]>; + + def ASTCXXDestructorDeclInterface : AttrInterface<"ASTCXXDestructorDeclInterface", + [ASTCXXMethodDeclInterface]>; + + def ASTTypeDeclInterface : AttrInterface<"ASTTypeDeclInterface", + [ASTNamedDeclInterface]>; + + def ASTTagDeclInterface : AttrInterface<"ASTTagDeclInterface", + [ASTTypeDeclInterface, ASTDeclContextInterface]> { + let methods = [ + InterfaceMethod<"", "clang::TagTypeKind", "getTagKind", (ins), [{}], + /*defaultImplementation=*/ [{ + return $_attr.getAstDecl()->getTagKind(); + }] + > + ]; + } + + def ASTEnumDeclInterface : AttrInterface<"ASTEnumDeclInterface", + [ASTTagDeclInterface]>; + + def ASTRecordDeclInterface : AttrInterface<"ASTRecordDeclInterface", + [ASTTagDeclInterface]>; + + def ASTCXXRecordDeclInterface : AttrInterface<"ASTCXXRecordDeclInterface", + [ASTRecordDeclInterface]> { + let methods = [ + InterfaceMethod<"", "bool", "isLambda", (ins), [{}], + /*defaultImplementation=*/ [{ + return $_attr.getAstDecl()->isLambda(); + }] + > + ]; + } + + def ASTClassTemplateSpecializationDeclInterface : + AttrInterface<"ASTClassTemplateSpecializationDeclInterface", + [ASTCXXRecordDeclInterface]>; + + def ASTClassTemplatePartialSpecializationDeclInterface : + AttrInterface<"ASTClassTemplatePartialSpecializationDeclInterface", + [ASTClassTemplateSpecializationDeclInterface]>; + +} // namespace mlir::cir #endif // MLIR_CIR_INTERFACES_AST_ATTR_INTERFACES diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.h b/clang/lib/CIR/CodeGen/CIRGenBuilder.h index 49f253ab05cc..4d2619cb89be 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuilder.h +++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.h @@ -413,25 +413,22 @@ class CIRGenBuilderTy : public mlir::OpBuilder { /// Get a CIR named struct type. mlir::cir::StructType getStructTy(llvm::ArrayRef members, llvm::StringRef name, bool body, - bool packed, mlir::Attribute astAttr) { + bool packed, mlir::Attribute ast) { const auto nameAttr = getStringAttr(name); auto kind = mlir::cir::StructType::RecordKind::Struct; - if (astAttr) { - if (auto tagDecl = mlir::dyn_cast< mlir::cir::ASTTagDeclInterface >(astAttr)) { + if (ast) + if (auto tagDecl = mlir::dyn_cast(ast)) kind = getRecordKind(tagDecl.getTagKind()); - } - } return mlir::cir::StructType::get(getContext(), members, nameAttr, body, - packed, kind, astAttr); + packed, kind, ast); } mlir::cir::StructType getStructTy(llvm::ArrayRef members, llvm::StringRef name, bool body, bool packed, const clang::RecordDecl *ast) { mlir::Attribute astAttr; - if (ast) { + if (ast) astAttr = mlir::cir::makeAstDeclAttr(ast, getContext()); - } return getStructTy(members, name, body, packed, astAttr); } diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp index b265bc4ae850..6d478265d533 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp @@ -132,8 +132,8 @@ static Address buildPointerWithAlignment(const Expr *E, if (BaseInfo) *BaseInfo = InnerBaseInfo; - if (isa(CE)) { - assert(!UnimplementedFeature::tbaa()); + if (isa(CE)) { + assert(!UnimplementedFeature::tbaa()); LValueBaseInfo TargetTypeBaseInfo; CharUnits Align = CGF.CGM.getNaturalPointeeTypeAlignment( @@ -2188,7 +2188,8 @@ mlir::Value CIRGenFunction::buildAlloca(StringRef name, mlir::Type ty, alignIntAttr); if (currVarDecl) { auto alloca = cast(addr.getDefiningOp()); - alloca.setAstAttr(mlir::cir::makeAstDeclAttr(currVarDecl, builder.getContext())); + alloca.setAstAttr( + mlir::cir::makeAstDeclAttr(currVarDecl, builder.getContext())); } } return addr; diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index 5b53f4026ac8..62f39726f9c2 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -194,7 +194,8 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &context, CIRGenModule::~CIRGenModule() {} -bool CIRGenModule::isTypeConstant(QualType Ty, bool ExcludeCtor, bool ExcludeDtor) { +bool CIRGenModule::isTypeConstant(QualType Ty, bool ExcludeCtor, + bool ExcludeDtor) { if (!Ty.isConstant(astCtx) && !Ty->isReferenceType()) return false; @@ -711,7 +712,7 @@ CIRGenModule::getAddrOfGlobalVarAttr(const VarDecl *D, mlir::Type Ty, return builder.getGlobalViewAttr(builder.getPointerTo(Ty), globalOp); } -mlir::Operation* CIRGenModule::getWeakRefReference(const ValueDecl *VD) { +mlir::Operation *CIRGenModule::getWeakRefReference(const ValueDecl *VD) { const AliasAttr *AA = VD->getAttr(); assert(AA && "No alias?"); @@ -726,8 +727,8 @@ mlir::Operation* CIRGenModule::getWeakRefReference(const ValueDecl *VD) { mlir::Type DeclTy = getTypes().convertTypeForMem(VD->getType()); if (DeclTy.isa()) { auto F = GetOrCreateCIRFunction(AA->getAliasee(), DeclTy, - GlobalDecl(cast(VD)), - /*ForVtable=*/false); + GlobalDecl(cast(VD)), + /*ForVtable=*/false); F.setLinkage(mlir::cir::GlobalLinkageKind::ExternalWeakLinkage); WeakRefReferences.insert(F); return F; @@ -1804,9 +1805,8 @@ CIRGenModule::createCIRFunction(mlir::Location loc, StringRef name, f = builder.create(loc, name, Ty); - if (FD) { + if (FD) f.setAstAttr(makeAstDeclAttr(FD, builder.getContext())); - } if (FD && !FD->hasPrototype()) f.setNoProtoAttr(builder.getUnitAttr()); @@ -1848,7 +1848,7 @@ mlir::Location CIRGenModule::getLocForFunction(const clang::FunctionDecl *FD) { } void CIRGenModule::setExtraAttributesForFunc(FuncOp f, - const clang::FunctionDecl *FD) { + const clang::FunctionDecl *FD) { mlir::NamedAttrList attrs; if (!FD) { @@ -1923,8 +1923,7 @@ void CIRGenModule::setExtraAttributesForFunc(FuncOp f, } f.setExtraAttrsAttr(mlir::cir::ExtraFuncAttributesAttr::get( - builder.getContext(), - attrs.getDictionary(builder.getContext()))); + builder.getContext(), attrs.getDictionary(builder.getContext()))); } /// If the specified mangled name is not in the module, diff --git a/clang/lib/CIR/CodeGen/CIRGenTypes.cpp b/clang/lib/CIR/CodeGen/CIRGenTypes.cpp index 2d1fd912f367..e9a5fe9a4ee0 100644 --- a/clang/lib/CIR/CodeGen/CIRGenTypes.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenTypes.cpp @@ -167,8 +167,9 @@ mlir::Type CIRGenTypes::convertRecordDeclType(const clang::RecordDecl *RD) { // Handle forward decl / incomplete types. if (!entry) { auto name = getRecordTypeName(RD, ""); - entry = Builder.getStructTy({}, name, /*body=*/false, /*packed=*/false, - mlir::cir::makeAstDeclAttr(RD, &getMLIRContext())); + entry = + Builder.getStructTy({}, name, /*body=*/false, /*packed=*/false, + mlir::cir::makeAstDeclAttr(RD, &getMLIRContext())); recordDeclTypes[key] = entry; } diff --git a/clang/lib/CIR/CodeGen/CIRRecordLayoutBuilder.cpp b/clang/lib/CIR/CodeGen/CIRRecordLayoutBuilder.cpp index a3f96d08fd9e..935d0db3e502 100644 --- a/clang/lib/CIR/CodeGen/CIRRecordLayoutBuilder.cpp +++ b/clang/lib/CIR/CodeGen/CIRRecordLayoutBuilder.cpp @@ -605,9 +605,10 @@ CIRGenTypes::computeRecordLayout(const RecordDecl *D, builder.astRecordLayout.getSize()) { CIRRecordLowering baseBuilder(*this, D, /*Packed=*/builder.isPacked); auto baseIdentifier = getRecordTypeName(D, ".base"); - *BaseTy = Builder.getStructTy(baseBuilder.fieldTypes, baseIdentifier, - /*body=*/true, /*packed=*/false, - mlir::cir::makeAstDeclAttr(D, &getMLIRContext())); + *BaseTy = + Builder.getStructTy(baseBuilder.fieldTypes, baseIdentifier, + /*body=*/true, /*packed=*/false, + mlir::cir::makeAstDeclAttr(D, &getMLIRContext())); // TODO(cir): add something like addRecordTypeName // BaseTy and Ty must agree on their packedness for getCIRFieldNo to work diff --git a/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp b/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp index c2ba79f6d98b..b80f41178631 100644 --- a/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp @@ -32,16 +32,15 @@ #include "clang/AST/Decl.h" #include "clang/AST/DeclCXX.h" -static void printStructMembers(mlir::AsmPrinter &p, - mlir::ArrayAttr members); +static void printStructMembers(mlir::AsmPrinter &p, mlir::ArrayAttr members); static mlir::ParseResult parseStructMembers(::mlir::AsmParser &parser, - mlir::ArrayAttr &members); + mlir::ArrayAttr &members); -llvm::SmallVector< mlir::cir::ASTCXXRecordDeclInterface, 4 > +llvm::SmallVector decls(const clang::DeclContext *ast, mlir::MLIRContext *ctx); -template< typename DeclType > -llvm::SmallVector< mlir::cir::ASTCXXRecordDeclInterface, 4 > +template +llvm::SmallVector decls(const DeclType *ast, mlir::MLIRContext *ctx); #define GET_ATTRDEF_CLASSES @@ -54,34 +53,36 @@ using namespace mlir::cir; // CIR AST Attr helpers //===----------------------------------------------------------------------===// -mlir::Attribute mlir::cir::makeAstDeclAttr(const clang::Decl *decl, mlir::MLIRContext *ctx) { - if (auto ast = clang::dyn_cast(decl)) - return ASTCXXConstructorDeclAttr::get(ctx, ast); - if (auto ast = clang::dyn_cast(decl)) - return ASTCXXConversionDeclAttr::get(ctx, ast); - if (auto ast = clang::dyn_cast(decl)) - return ASTCXXDestructorDeclAttr::get(ctx, ast); - if (auto ast = clang::dyn_cast(decl)) - return ASTCXXMethodDeclAttr::get(ctx, ast); - if (auto ast = clang::dyn_cast(decl)) - return ASTFunctionDeclAttr::get(ctx, ast); - if (auto ast = clang::dyn_cast(decl)) - return ASTClassTemplatePartialSpecializationDeclAttr::get(ctx, ast); - if (auto ast = clang::dyn_cast(decl)) - return ASTClassTemplateSpecializationDeclAttr::get(ctx, ast); - if (auto ast = clang::dyn_cast(decl)) - return ASTCXXRecordDeclAttr::get(ctx, ast); - if (auto ast = clang::dyn_cast(decl)) - return ASTRecordDeclAttr::get(ctx, ast); - if (auto ast = clang::dyn_cast(decl)) - return ASTEnumDeclAttr::get(ctx, ast); - if (auto ast = clang::dyn_cast(decl)) - return ASTTagDeclAttr::get(ctx, ast); - if (auto ast = clang::dyn_cast(decl)) - return ASTTypeDeclAttr::get(ctx, ast); - if (auto ast = clang::dyn_cast(decl)) - return ASTVarDeclAttr::get(ctx, ast); - return ASTDeclAttr::get(ctx, decl); +mlir::Attribute mlir::cir::makeAstDeclAttr(const clang::Decl *decl, + mlir::MLIRContext *ctx) { + if (auto ast = clang::dyn_cast(decl)) + return ASTCXXConstructorDeclAttr::get(ctx, ast); + if (auto ast = clang::dyn_cast(decl)) + return ASTCXXConversionDeclAttr::get(ctx, ast); + if (auto ast = clang::dyn_cast(decl)) + return ASTCXXDestructorDeclAttr::get(ctx, ast); + if (auto ast = clang::dyn_cast(decl)) + return ASTCXXMethodDeclAttr::get(ctx, ast); + if (auto ast = clang::dyn_cast(decl)) + return ASTFunctionDeclAttr::get(ctx, ast); + if (auto ast = + clang::dyn_cast(decl)) + return ASTClassTemplatePartialSpecializationDeclAttr::get(ctx, ast); + if (auto ast = clang::dyn_cast(decl)) + return ASTClassTemplateSpecializationDeclAttr::get(ctx, ast); + if (auto ast = clang::dyn_cast(decl)) + return ASTCXXRecordDeclAttr::get(ctx, ast); + if (auto ast = clang::dyn_cast(decl)) + return ASTRecordDeclAttr::get(ctx, ast); + if (auto ast = clang::dyn_cast(decl)) + return ASTEnumDeclAttr::get(ctx, ast); + if (auto ast = clang::dyn_cast(decl)) + return ASTTagDeclAttr::get(ctx, ast); + if (auto ast = clang::dyn_cast(decl)) + return ASTTypeDeclAttr::get(ctx, ast); + if (auto ast = clang::dyn_cast(decl)) + return ASTVarDeclAttr::get(ctx, ast); + return ASTDeclAttr::get(ctx, decl); }; //===----------------------------------------------------------------------===// @@ -107,14 +108,14 @@ void CIRDialect::printAttribute(Attribute attr, DialectAsmPrinter &os) const { } static void printStructMembers(mlir::AsmPrinter &printer, - mlir::ArrayAttr members) { + mlir::ArrayAttr members) { printer << '{'; llvm::interleaveComma(members, printer); printer << '}'; } static ParseResult parseStructMembers(mlir::AsmParser &parser, - mlir::ArrayAttr &members) { + mlir::ArrayAttr &members) { SmallVector elts; auto delimiter = AsmParser::Delimiter::Braces; diff --git a/clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp b/clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp index 57bca6060dff..4f953ef32047 100644 --- a/clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp +++ b/clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp @@ -106,10 +106,8 @@ struct LifetimeCheckPass : public LifetimeCheckBase { // Common helpers. bool isCtorInitPointerFromOwner(CallOp callOp); - mlir::Value getNonConstUseOfOwner(CallOp callOp, - ASTCXXMethodDeclInterface m); - bool isOwnerOrPointerClassMethod(CallOp callOp, - ASTCXXMethodDeclInterface m); + mlir::Value getNonConstUseOfOwner(CallOp callOp, ASTCXXMethodDeclInterface m); + bool isOwnerOrPointerClassMethod(CallOp callOp, ASTCXXMethodDeclInterface m); // Diagnostic helpers. void emitInvalidHistory(mlir::InFlightDiagnostic &D, mlir::Value histKey, @@ -895,7 +893,7 @@ template bool isStructAndHasAttr(mlir::Type ty) { return false; auto sTy = ty.cast(); auto recordDecl = sTy.getAst(); - if (auto interface = dyn_cast< ASTDeclInterface >(recordDecl)) + if (auto interface = dyn_cast(recordDecl)) if (hasAttr(interface)) return true; return false; @@ -1486,7 +1484,7 @@ static const ASTCXXMethodDeclInterface getMethod(ModuleOp mod, CallOp callOp) { auto method = getCalleeFromSymbol(mod, name); if (!method || method.getBuiltin()) return nullptr; - return dyn_cast< ASTCXXMethodDeclInterface >(method.getAstAttr()); + return dyn_cast(method.getAstAttr()); } mlir::Value LifetimeCheckPass::getThisParamPointerCategory(CallOp callOp) { @@ -1764,7 +1762,7 @@ bool LifetimeCheckPass::isLambdaType(mlir::Type ty) { auto taskTy = ty.dyn_cast(); if (!taskTy) return false; - if (auto recordDecl = dyn_cast< ASTCXXRecordDeclInterface >(taskTy.getAst())) + if (auto recordDecl = dyn_cast(taskTy.getAst())) if (recordDecl.isLambda()) IsLambdaTyCache[ty] = true; @@ -1781,12 +1779,12 @@ bool LifetimeCheckPass::isTaskType(mlir::Value taskVal) { if (!taskTy) return false; auto recordDecl = taskTy.getAst(); - auto spec = dyn_cast< ASTClassTemplateSpecializationDeclInterface >(recordDecl); + auto spec = dyn_cast(recordDecl); if (!spec) return false; for (auto sub : spec.decls(taskVal.getContext())) { - if (auto subRec = dyn_cast< ASTCXXRecordDeclInterface >(sub)) { + if (auto subRec = dyn_cast(sub)) { if (subRec.getDeclName().isIdentifier() && subRec.getName() == "promise_type") { IsTaskTyCache[ty] = true; @@ -1835,7 +1833,7 @@ void LifetimeCheckPass::checkCall(CallOp callOp) { // From this point on only owner and pointer class methods handling, // starting from special methods. - if (auto ctor = dyn_cast< ASTCXXConstructorDeclInterface >(methodDecl)) + if (auto ctor = dyn_cast(methodDecl)) return checkCtor(callOp, ctor); if (methodDecl.isMoveAssignmentOperator()) return checkMoveAssignment(callOp, methodDecl); diff --git a/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp b/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp index 5994710aadc3..670405d9b278 100644 --- a/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp +++ b/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp @@ -14,8 +14,8 @@ #include "clang/AST/Mangle.h" #include "clang/Basic/Module.h" #include "clang/CIR/Dialect/IR/CIRDialect.h" -#include "clang/CIR/Interfaces/ASTAttrInterfaces.h" #include "clang/CIR/Dialect/Passes.h" +#include "clang/CIR/Interfaces/ASTAttrInterfaces.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringRef.h" @@ -137,7 +137,7 @@ cir::FuncOp LoweringPreparePass::buildCXXGlobalVarDeclInitFunc(GlobalOp op) { SmallString<256> fnName; { llvm::raw_svector_ostream Out(fnName); - mlir::cast< ASTVarDeclInterface >(*op.getAst()).mangleDynamicInitializer(Out); + mlir::cast(*op.getAst()).mangleDynamicInitializer(Out); // Name numbering uint32_t cnt = dynamicInitializerNames[fnName]++; if (cnt) @@ -151,7 +151,7 @@ cir::FuncOp LoweringPreparePass::buildCXXGlobalVarDeclInitFunc(GlobalOp op) { auto fnType = mlir::cir::FuncType::get({}, voidTy); FuncOp f = buildRuntimeFunction(builder, fnName, op.getLoc(), fnType, - mlir::cir::GlobalLinkageKind::InternalLinkage); + mlir::cir::GlobalLinkageKind::InternalLinkage); // Move over the initialzation code of the ctor region. auto &block = op.getCtorRegion().front(); @@ -159,12 +159,12 @@ cir::FuncOp LoweringPreparePass::buildCXXGlobalVarDeclInitFunc(GlobalOp op) { entryBB->getOperations().splice(entryBB->begin(), block.getOperations(), block.begin(), std::prev(block.end())); - // Register the destructor call with __cxa_atexit - assert(mlir::isa< ASTVarDeclInterface >(*op.getAst()) && - mlir::dyn_cast< ASTVarDeclInterface >(*op.getAst()).getTLSKind() == - clang::VarDecl::TLS_None && " TLS NYI"); + assert(mlir::isa(*op.getAst()) && + mlir::dyn_cast(*op.getAst()).getTLSKind() == + clang::VarDecl::TLS_None && + " TLS NYI"); // Create a variable that binds the atexit to this shared object. builder.setInsertionPointToStart(&theModule.getBodyRegion().front()); auto Handle = buildRuntimeVariable(builder, "__dso_handle", op.getLoc(), @@ -180,7 +180,7 @@ cir::FuncOp LoweringPreparePass::buildCXXGlobalVarDeclInitFunc(GlobalOp op) { assert(dtorCall && "Expected a dtor call"); cir::FuncOp dtorFunc = getCalledFunction(dtorCall); assert(dtorFunc && - mlir::isa< ASTCXXDestructorDeclInterface >(*dtorFunc.getAst()) && + mlir::isa(*dtorFunc.getAst()) && "Expected a dtor call"); // Create a runtime helper function: @@ -198,7 +198,8 @@ cir::FuncOp LoweringPreparePass::buildCXXGlobalVarDeclInitFunc(GlobalOp op) { FuncOp fnAtExit = buildRuntimeFunction(builder, nameAtExit, op.getLoc(), fnAtExitType); - // Replace the dtor call with a call to __cxa_atexit(&dtor, &var, &__dso_handle) + // Replace the dtor call with a call to __cxa_atexit(&dtor, &var, + // &__dso_handle) builder.setInsertionPointAfter(dtorCall); mlir::Value args[3]; auto dtorPtrTy = mlir::cir::PointerType::get(builder.getContext(), @@ -242,7 +243,7 @@ void LoweringPreparePass::lowerGlobalOp(GlobalOp op) { // Add a function call to the variable initialization function. assert(!hasAttr( - mlir::cast(*op.getAst())) && + mlir::cast(*op.getAst())) && "custom initialization priority NYI"); dynamicInitializers.push_back(f); } @@ -255,8 +256,7 @@ void LoweringPreparePass::buildCXXGlobalInitFunc() { SmallVector attrs; for (auto &f : dynamicInitializers) { // TODO: handle globals with a user-specified initialzation priority. - auto ctorAttr = - mlir::cir::GlobalCtorAttr::get(&getContext(), f.getName()); + auto ctorAttr = mlir::cir::GlobalCtorAttr::get(&getContext(), f.getName()); attrs.push_back(ctorAttr); } @@ -287,7 +287,7 @@ void LoweringPreparePass::buildCXXGlobalInitFunc() { {}, mlir::cir::VoidType::get(builder.getContext())); FuncOp f = buildRuntimeFunction(builder, fnName, theModule.getLoc(), fnType, - mlir::cir::GlobalLinkageKind::ExternalLinkage); + mlir::cir::GlobalLinkageKind::ExternalLinkage); builder.setInsertionPointToStart(f.addEntryBlock()); for (auto &f : dynamicInitializers) { builder.create(f.getLoc(), f); @@ -305,7 +305,7 @@ void LoweringPreparePass::runOnOp(Operation *op) { void LoweringPreparePass::runOnOperation() { assert(astCtx && "Missing ASTContext, please construct with the right ctor"); - auto* op = getOperation(); + auto *op = getOperation(); if (isa<::mlir::ModuleOp>(op)) { theModule = cast<::mlir::ModuleOp>(op); } @@ -327,7 +327,8 @@ std::unique_ptr mlir::createLoweringPreparePass() { return std::make_unique(); } -std::unique_ptr mlir::createLoweringPreparePass(clang::ASTContext *astCtx) { +std::unique_ptr +mlir::createLoweringPreparePass(clang::ASTContext *astCtx) { auto pass = std::make_unique(); pass->setASTContext(astCtx); return std::move(pass); From 94c26b224a6cc48d2f8481d241787cfc5c0a8497 Mon Sep 17 00:00:00 2001 From: Henrich Lauko Date: Thu, 7 Sep 2023 11:53:08 +0200 Subject: [PATCH 20/25] [CIR] Add interface method to query promise type existence. --- .../clang/CIR/Interfaces/ASTAttrInterfaces.td | 37 ++++++++++--------- clang/lib/CIR/Dialect/IR/CIRAttrs.cpp | 7 ---- .../CIR/Dialect/Transforms/LifetimeCheck.cpp | 31 ++++++---------- 3 files changed, 31 insertions(+), 44 deletions(-) diff --git a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td index 1e4dc3ed190d..12dcdc13c37c 100644 --- a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td +++ b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td @@ -32,21 +32,6 @@ let cppNamespace = "::mlir::cir" in { ]; } - def ASTDeclContextInterface : AttrInterface<"ASTDeclContextInterface"> { - let methods = [ - InterfaceMethod<"", "llvm::SmallVector< ::mlir::Attribute, 4 >", "decls", - (ins "::mlir::MLIRContext *":$ctx), [{}], - /*defaultImplementation=*/ [{ - llvm::SmallVector< mlir::Attribute, 4 > decls; - for (const auto *sub : $_attr.getAstDecl()->decls()) { - decls.push_back(makeAstDeclAttr(sub, ctx)); - } - return decls; - }] - > - ]; - } - def ASTNamedDeclInterface : AttrInterface<"ASTNamedDeclInterface", [ASTDeclInterface]> { let methods = [ @@ -88,7 +73,7 @@ let cppNamespace = "::mlir::cir" in { } def ASTFunctionDeclInterface : AttrInterface<"ASTFunctionDeclInterface", - [ASTDeclaratorDeclInterface, ASTDeclContextInterface]> { + [ASTDeclaratorDeclInterface]> { let methods = [ InterfaceMethod<"", "bool", "isOverloadedOperator", (ins), [{}], /*defaultImplementation=*/ [{ @@ -154,7 +139,7 @@ let cppNamespace = "::mlir::cir" in { [ASTNamedDeclInterface]>; def ASTTagDeclInterface : AttrInterface<"ASTTagDeclInterface", - [ASTTypeDeclInterface, ASTDeclContextInterface]> { + [ASTTypeDeclInterface]> { let methods = [ InterfaceMethod<"", "clang::TagTypeKind", "getTagKind", (ins), [{}], /*defaultImplementation=*/ [{ @@ -183,7 +168,23 @@ let cppNamespace = "::mlir::cir" in { def ASTClassTemplateSpecializationDeclInterface : AttrInterface<"ASTClassTemplateSpecializationDeclInterface", - [ASTCXXRecordDeclInterface]>; + [ASTCXXRecordDeclInterface]> { + let methods = [ + InterfaceMethod<"", "bool", "hasPromiseType", (ins), [{}], + /*defaultImplementation=*/ [{ + for (const auto *sub : $_attr.getAstDecl()->decls()) { + if (auto subRec = clang::dyn_cast(sub)) { + if (subRec->getDeclName().isIdentifier() && + subRec->getName() == "promise_type") { + return true; + } + } + } + return false; + }] + > + ]; + } def ASTClassTemplatePartialSpecializationDeclInterface : AttrInterface<"ASTClassTemplatePartialSpecializationDeclInterface", diff --git a/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp b/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp index b80f41178631..40edc56da245 100644 --- a/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp @@ -36,13 +36,6 @@ static void printStructMembers(mlir::AsmPrinter &p, mlir::ArrayAttr members); static mlir::ParseResult parseStructMembers(::mlir::AsmParser &parser, mlir::ArrayAttr &members); -llvm::SmallVector -decls(const clang::DeclContext *ast, mlir::MLIRContext *ctx); - -template -llvm::SmallVector -decls(const DeclType *ast, mlir::MLIRContext *ctx); - #define GET_ATTRDEF_CLASSES #include "clang/CIR/Dialect/IR/CIROpsAttributes.cpp.inc" diff --git a/clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp b/clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp index 4f953ef32047..4a5ece6d8c06 100644 --- a/clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp +++ b/clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp @@ -1774,26 +1774,19 @@ bool LifetimeCheckPass::isTaskType(mlir::Value taskVal) { if (IsTaskTyCache.count(ty)) return IsTaskTyCache[ty]; - IsTaskTyCache[ty] = false; - auto taskTy = taskVal.getType().dyn_cast(); - if (!taskTy) - return false; - auto recordDecl = taskTy.getAst(); - auto spec = dyn_cast(recordDecl); - if (!spec) - return false; - - for (auto sub : spec.decls(taskVal.getContext())) { - if (auto subRec = dyn_cast(sub)) { - if (subRec.getDeclName().isIdentifier() && - subRec.getName() == "promise_type") { - IsTaskTyCache[ty] = true; - break; - } - } - } + bool result = [&] { + auto taskTy = taskVal.getType().dyn_cast(); + if (!taskTy) + return false; + auto recordDecl = taskTy.getAst(); + auto spec = dyn_cast(recordDecl); + if (!spec) + return false; + return spec.hasPromiseType(); + } (); - return IsTaskTyCache[ty]; + IsTaskTyCache[ty] = result; + return result; } void LifetimeCheckPass::trackCallToCoroutine(CallOp callOp) { From 82c8886962620ed9f0ffcc04355ef91c4d30ecbe Mon Sep 17 00:00:00 2001 From: Henrich Lauko Date: Thu, 7 Sep 2023 13:11:43 +0200 Subject: [PATCH 21/25] [CIR] Use ASTVarDeclInterface to constrain AST in VarOp and GlobalOp. --- clang/include/clang/CIR/Dialect/IR/CIROps.td | 4 ++-- clang/lib/CIR/CodeGen/CIRGenCXX.cpp | 2 +- clang/lib/CIR/CodeGen/CIRGenExpr.cpp | 3 +-- clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp | 6 ++---- 4 files changed, 6 insertions(+), 9 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index 5bac707a6107..cb97f37b4dd8 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -295,7 +295,7 @@ def AllocaOp : CIR_Op<"alloca", [ StrAttr:$name, UnitAttr:$init, ConfinedAttr, [IntMinValue<0>]>:$alignment, - OptionalAttr:$ast + OptionalAttr:$ast ); let results = (outs Res:$initial_value, UnitAttr:$constant, OptionalAttr:$alignment, - OptionalAttr:$ast + OptionalAttr:$ast ); let regions = (region AnyRegion:$ctorRegion, AnyRegion:$dtorRegion); let assemblyFormat = [{ diff --git a/clang/lib/CIR/CodeGen/CIRGenCXX.cpp b/clang/lib/CIR/CodeGen/CIRGenCXX.cpp index 33d59d4d7207..d6c33dcd5ce7 100644 --- a/clang/lib/CIR/CodeGen/CIRGenCXX.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenCXX.cpp @@ -111,7 +111,7 @@ void CIRGenModule::codegenGlobalInitCxxStructor(const VarDecl *D, CIRGenFunction CGF{*this, builder, true}; CurCGF = &CGF; CurCGF->CurFn = Addr; - Addr.setAstAttr(mlir::cir::makeAstDeclAttr(D, builder.getContext())); + Addr.setAstAttr(mlir::cir::ASTVarDeclAttr::get(builder.getContext(), D)); if (NeedsCtor) { mlir::OpBuilder::InsertionGuard guard(builder); diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp index 6d478265d533..20afa615de87 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp @@ -2188,8 +2188,7 @@ mlir::Value CIRGenFunction::buildAlloca(StringRef name, mlir::Type ty, alignIntAttr); if (currVarDecl) { auto alloca = cast(addr.getDefiningOp()); - alloca.setAstAttr( - mlir::cir::makeAstDeclAttr(currVarDecl, builder.getContext())); + alloca.setAstAttr(ASTVarDeclAttr::get(builder.getContext(), currVarDecl)); } } return addr; diff --git a/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp b/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp index 670405d9b278..9e0b9ec4a203 100644 --- a/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp +++ b/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp @@ -137,7 +137,7 @@ cir::FuncOp LoweringPreparePass::buildCXXGlobalVarDeclInitFunc(GlobalOp op) { SmallString<256> fnName; { llvm::raw_svector_ostream Out(fnName); - mlir::cast(*op.getAst()).mangleDynamicInitializer(Out); + op.getAst()->mangleDynamicInitializer(Out); // Name numbering uint32_t cnt = dynamicInitializerNames[fnName]++; if (cnt) @@ -161,9 +161,7 @@ cir::FuncOp LoweringPreparePass::buildCXXGlobalVarDeclInitFunc(GlobalOp op) { // Register the destructor call with __cxa_atexit - assert(mlir::isa(*op.getAst()) && - mlir::dyn_cast(*op.getAst()).getTLSKind() == - clang::VarDecl::TLS_None && + assert(op.getAst() && op.getAst()->getTLSKind() == clang::VarDecl::TLS_None && " TLS NYI"); // Create a variable that binds the atexit to this shared object. builder.setInsertionPointToStart(&theModule.getBodyRegion().front()); From 784333ed4cfa9524d30a7fa0a86be748b9475ecc Mon Sep 17 00:00:00 2001 From: Henrich Lauko Date: Fri, 8 Sep 2023 09:16:15 +0200 Subject: [PATCH 22/25] [CIR] Add missing tablegen dependencies. --- clang/lib/CIR/CodeGen/CMakeLists.txt | 1 + clang/lib/CIR/FrontendAction/CMakeLists.txt | 3 +++ clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt | 3 +++ clang/lib/CIR/Lowering/ThroughMLIR/CMakeLists.txt | 3 +++ clang/lib/FrontendTool/CMakeLists.txt | 1 + 5 files changed, 11 insertions(+) diff --git a/clang/lib/CIR/CodeGen/CMakeLists.txt b/clang/lib/CIR/CodeGen/CMakeLists.txt index 8deb7caf45bf..a379ed464316 100644 --- a/clang/lib/CIR/CodeGen/CMakeLists.txt +++ b/clang/lib/CIR/CodeGen/CMakeLists.txt @@ -38,6 +38,7 @@ add_clang_library(clangCIR DEPENDS MLIRCIR MLIRCIROpsIncGen + MLIRCIRASTAttrInterfacesIncGen ${dialect_libs} LINK_LIBS diff --git a/clang/lib/CIR/FrontendAction/CMakeLists.txt b/clang/lib/CIR/FrontendAction/CMakeLists.txt index b2655831fcc6..7201db6502e6 100644 --- a/clang/lib/CIR/FrontendAction/CMakeLists.txt +++ b/clang/lib/CIR/FrontendAction/CMakeLists.txt @@ -11,6 +11,9 @@ add_clang_library(clangCIRFrontendAction DEPENDS MLIRCIROpsIncGen MLIRCIRASTAttrInterfacesIncGen + MLIRBuiltinLocationAttributesIncGen + MLIRBuiltinTypeInterfacesIncGen + MLIRFunctionInterfacesIncGen LINK_LIBS clangAST diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt b/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt index 5ffc4cefc6cd..b252af37dace 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt +++ b/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt @@ -13,6 +13,9 @@ add_clang_library(clangCIRLoweringDirectToLLVM MLIRCIREnumsGen MLIRCIROpsIncGen MLIRCIRASTAttrInterfacesIncGen + MLIRBuiltinLocationAttributesIncGen + MLIRBuiltinTypeInterfacesIncGen + MLIRFunctionInterfacesIncGen LINK_LIBS clangAST diff --git a/clang/lib/CIR/Lowering/ThroughMLIR/CMakeLists.txt b/clang/lib/CIR/Lowering/ThroughMLIR/CMakeLists.txt index b0942512e1b8..b130d2ea4807 100644 --- a/clang/lib/CIR/Lowering/ThroughMLIR/CMakeLists.txt +++ b/clang/lib/CIR/Lowering/ThroughMLIR/CMakeLists.txt @@ -13,6 +13,9 @@ add_clang_library(clangCIRLoweringThroughMLIR MLIRCIROpsIncGen MLIRCIREnumsGen MLIRCIRASTAttrInterfacesIncGen + MLIRBuiltinLocationAttributesIncGen + MLIRBuiltinTypeInterfacesIncGen + MLIRFunctionInterfacesIncGen LINK_LIBS clangAST diff --git a/clang/lib/FrontendTool/CMakeLists.txt b/clang/lib/FrontendTool/CMakeLists.txt index ceb4d3f91b68..6dae1455010c 100644 --- a/clang/lib/FrontendTool/CMakeLists.txt +++ b/clang/lib/FrontendTool/CMakeLists.txt @@ -23,6 +23,7 @@ if(CLANG_ENABLE_CIR) ) list(APPEND deps MLIRBuiltinLocationAttributesIncGen + MLIRBuiltinTypeInterfacesIncGen ) include_directories(${LLVM_MAIN_SRC_DIR}/../mlir/include) From 81e9219916190512256d63ccdf83df811382f498 Mon Sep 17 00:00:00 2001 From: Henrich Lauko Date: Fri, 8 Sep 2023 09:19:07 +0200 Subject: [PATCH 23/25] [CIR] Make AST Function attributes constrained. --- clang/include/clang/CIR/Dialect/IR/CIROps.td | 2 +- .../clang/CIR/Interfaces/ASTAttrInterfaces.h | 3 ++ .../clang/CIR/Interfaces/ASTAttrInterfaces.td | 10 ++++++ clang/lib/CIR/CodeGen/CIRGenModule.cpp | 2 +- clang/lib/CIR/Dialect/IR/CIRAttrs.cpp | 34 +++++++++++++++++-- clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 1 + 6 files changed, 48 insertions(+), 4 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index cb97f37b4dd8..13e560f4ff4c 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -1592,7 +1592,7 @@ def FuncOp : CIR_Op<"func", [ OptionalAttr:$arg_attrs, OptionalAttr:$res_attrs, OptionalAttr:$aliasee, - OptionalAttr:$ast); + OptionalAttr:$ast); let regions = (region AnyRegion:$body); let skipDefaultBuilders = 1; diff --git a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h index 06fd329e8725..cf77ee5c39e0 100644 --- a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h +++ b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h @@ -21,6 +21,9 @@ namespace cir { mlir::Attribute makeAstDeclAttr(const clang::Decl *decl, mlir::MLIRContext *ctx); +mlir::Attribute makeFuncDeclAttr(const clang::Decl *decl, + mlir::MLIRContext *ctx); + } // namespace cir } // namespace mlir diff --git a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td index 12dcdc13c37c..3dcaa13172f1 100644 --- a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td +++ b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td @@ -190,6 +190,16 @@ let cppNamespace = "::mlir::cir" in { AttrInterface<"ASTClassTemplatePartialSpecializationDeclInterface", [ASTClassTemplateSpecializationDeclInterface]>; + + def AnyASTFunctionDeclAttr : Attr< + CPred<"::mlir::isa<::mlir::cir::ASTFunctionDeclInterface>($_self)">, + "AST Function attribute"> { + let storageType = "::mlir::Attribute"; + let returnType = "::mlir::Attribute"; + let convertFromStorage = "$_self"; + let constBuilderCall = "$0"; + } + } // namespace mlir::cir #endif // MLIR_CIR_INTERFACES_AST_ATTR_INTERFACES diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index 62f39726f9c2..2c85d8fa1e13 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -1806,7 +1806,7 @@ CIRGenModule::createCIRFunction(mlir::Location loc, StringRef name, f = builder.create(loc, name, Ty); if (FD) - f.setAstAttr(makeAstDeclAttr(FD, builder.getContext())); + f.setAstAttr(makeFuncDeclAttr(FD, builder.getContext())); if (FD && !FD->hasPrototype()) f.setNoProtoAttr(builder.getUnitAttr()); diff --git a/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp b/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp index 40edc56da245..d862fc65e74f 100644 --- a/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp @@ -46,8 +46,11 @@ using namespace mlir::cir; // CIR AST Attr helpers //===----------------------------------------------------------------------===// -mlir::Attribute mlir::cir::makeAstDeclAttr(const clang::Decl *decl, - mlir::MLIRContext *ctx) { +namespace mlir { +namespace cir { + +mlir::Attribute makeAstDeclAttr(const clang::Decl *decl, + mlir::MLIRContext *ctx) { if (auto ast = clang::dyn_cast(decl)) return ASTCXXConstructorDeclAttr::get(ctx, ast); if (auto ast = clang::dyn_cast(decl)) @@ -78,6 +81,33 @@ mlir::Attribute mlir::cir::makeAstDeclAttr(const clang::Decl *decl, return ASTDeclAttr::get(ctx, decl); }; +mlir::Attribute makeFuncDeclAttr(const clang::Decl *decl, + mlir::MLIRContext *ctx) { + return llvm::TypeSwitch(decl) + .Case([ctx](const clang::CXXConstructorDecl *ast) { + return ASTCXXConstructorDeclAttr::get(ctx, ast); + }) + .Case([ctx](const clang::CXXConversionDecl *ast) { + return ASTCXXConversionDeclAttr::get(ctx, ast); + }) + .Case([ctx](const clang::CXXDestructorDecl *ast) { + return ASTCXXDestructorDeclAttr::get(ctx, ast); + }) + .Case([ctx](const clang::CXXMethodDecl *ast) { + return ASTCXXMethodDeclAttr::get(ctx, ast); + }) + .Case([ctx](const clang::FunctionDecl *ast) { + return ASTFunctionDeclAttr::get(ctx, ast); + }) + .Default([](auto) { + llvm_unreachable("unexpected Decl kind"); + return mlir::Attribute(); + }); +} + +} // namespace cir +} // namespace mlir + //===----------------------------------------------------------------------===// // General CIR parsing / printing //===----------------------------------------------------------------------===// diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp index a34aa3148c6b..48fe9bc43c1b 100644 --- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp @@ -42,6 +42,7 @@ using namespace mlir::cir; #include "clang/CIR/Dialect/IR/CIROpsStructs.cpp.inc" #include "clang/CIR/Dialect/IR/CIROpsDialect.cpp.inc" +#include "clang/CIR/Interfaces/ASTAttrInterfaces.h" //===----------------------------------------------------------------------===// // CIR Dialect From 019b556791f11809343ead5d18655e97804fe8a4 Mon Sep 17 00:00:00 2001 From: Henrich Lauko Date: Fri, 8 Sep 2023 15:10:11 +0200 Subject: [PATCH 24/25] [CIR] Refactor AST record interface and make it more type safe. --- .../include/clang/CIR/Dialect/IR/CIRAttrs.td | 15 ------- .../include/clang/CIR/Dialect/IR/CIRTypes.td | 4 +- .../clang/CIR/Interfaces/ASTAttrInterfaces.h | 3 -- .../clang/CIR/Interfaces/ASTAttrInterfaces.td | 40 ++++++------------- clang/lib/CIR/CodeGen/CIRGenBuilder.h | 23 ++++------- clang/lib/CIR/CodeGen/CIRGenTypes.cpp | 4 +- .../CIR/CodeGen/CIRRecordLayoutBuilder.cpp | 9 ++--- clang/lib/CIR/Dialect/IR/CIRAttrs.cpp | 32 --------------- clang/lib/CIR/Dialect/IR/CIRTypes.cpp | 6 +-- .../CIR/Dialect/Transforms/LifetimeCheck.cpp | 18 ++------- clang/test/CIR/CodeGen/bitfields.cpp | 2 +- clang/test/CIR/CodeGen/dtors.cpp | 2 +- clang/test/CIR/CodeGen/struct.cpp | 2 +- clang/test/CIR/CodeGen/union.cpp | 12 +++--- clang/test/CIR/CodeGen/vtable-rtti.cpp | 2 +- clang/test/CIR/IR/global.cir | 2 +- clang/test/CIR/IR/invalid.cir | 4 +- clang/test/CIR/IR/struct.cir | 2 +- clang/test/CIR/Lowering/array.cir | 2 +- clang/test/CIR/Lowering/struct.cir | 8 ++-- clang/test/CIR/Lowering/unions.cir | 6 +-- 21 files changed, 56 insertions(+), 142 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td index 212638a0f434..8f72062c0c04 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td @@ -444,24 +444,9 @@ def ASTTypeDeclAttr: ASTDecl<"TypeDecl", "type.decl", def ASTTagDeclAttr : ASTDecl<"TagDecl", "tag.decl", [ASTTagDeclInterface]>; -def ASTEnumDeclAttr : ASTDecl<"EnumDecl", "enum.decl", - [ASTEnumDeclInterface]>; - def ASTRecordDeclAttr : ASTDecl<"RecordDecl", "record.decl", [ASTRecordDeclInterface]>; -def ASTCXXRecordDeclAttr : ASTDecl<"CXXRecordDecl", "cxxrecord.decl", - [ASTCXXRecordDeclInterface]>; - -def ASTClassTemplateSpecializationDeclAttr : - ASTDecl<"ClassTemplateSpecializationDecl", "class.template.spec.decl", - [ASTClassTemplateSpecializationDeclInterface]>; - -def ASTClassTemplatePartialSpecializationDeclAttr : - ASTDecl<"ClassTemplatePartialSpecializationDecl", - "class.template.partial.spec.decl", - [ASTClassTemplatePartialSpecializationDeclInterface]>; - //===----------------------------------------------------------------------===// // ExtraFuncAttr //===----------------------------------------------------------------------===// diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td index 59aa4120608c..ddc8182da3b0 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td @@ -112,7 +112,7 @@ def CIR_StructType : CIR_Type<"Struct", "struct", "bool":$body, "bool":$packed, "mlir::cir::StructType::RecordKind":$kind, - OptionalParameter<"Attribute">:$ast + OptionalParameter<"std::optional">:$ast ); let hasCustomAssemblyFormat = 1; @@ -164,7 +164,7 @@ def CIR_StructType : CIR_Type<"Struct", "struct", let extraClassDefinition = [{ void $cppClass::dropAst() { - getImpl()->ast = Attribute(); + getImpl()->ast = std::nullopt; } }]; } diff --git a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h index cf77ee5c39e0..e2f1e16eb511 100644 --- a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h +++ b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h @@ -18,9 +18,6 @@ namespace mlir { namespace cir { -mlir::Attribute makeAstDeclAttr(const clang::Decl *decl, - mlir::MLIRContext *ctx); - mlir::Attribute makeFuncDeclAttr(const clang::Decl *decl, mlir::MLIRContext *ctx); diff --git a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td index 3dcaa13172f1..8aca1d9c8e63 100644 --- a/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td +++ b/clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td @@ -16,17 +16,17 @@ let cppNamespace = "::mlir::cir" in { let methods = [ InterfaceMethod<"", "bool", "hasOwnerAttr", (ins), [{}], /*defaultImplementation=*/ [{ - return $_attr.getAstDecl()->template hasAttr< clang::OwnerAttr >(); + return $_attr.getAstDecl()->template hasAttr(); }] >, InterfaceMethod<"", "bool", "hasPointerAttr", (ins), [{}], /*defaultImplementation=*/ [{ - return $_attr.getAstDecl()->template hasAttr< clang::PointerAttr >(); + return $_attr.getAstDecl()->template hasAttr(); }] >, InterfaceMethod<"", "bool", "hasInitPriorityAttr", (ins), [{}], /*defaultImplementation=*/ [{ - return $_attr.getAstDecl()->template hasAttr< clang::InitPriorityAttr >(); + return $_attr.getAstDecl()->template hasAttr(); }] > ]; @@ -93,14 +93,14 @@ let cppNamespace = "::mlir::cir" in { let methods = [ InterfaceMethod<"", "bool", "isCopyAssignmentOperator", (ins), [{}], /*defaultImplementation=*/ [{ - if (auto decl = dyn_cast< clang::CXXMethodDecl >($_attr.getAstDecl())) + if (auto decl = dyn_cast($_attr.getAstDecl())) return decl->isCopyAssignmentOperator(); return false; }] >, InterfaceMethod<"", "bool", "isMoveAssignmentOperator", (ins), [{}], /*defaultImplementation=*/ [{ - if (auto decl = dyn_cast< clang::CXXMethodDecl >($_attr.getAstDecl())) + if (auto decl = dyn_cast($_attr.getAstDecl())) return decl->isMoveAssignmentOperator(); return false; }] @@ -149,29 +149,20 @@ let cppNamespace = "::mlir::cir" in { ]; } - def ASTEnumDeclInterface : AttrInterface<"ASTEnumDeclInterface", - [ASTTagDeclInterface]>; - def ASTRecordDeclInterface : AttrInterface<"ASTRecordDeclInterface", - [ASTTagDeclInterface]>; - - def ASTCXXRecordDeclInterface : AttrInterface<"ASTCXXRecordDeclInterface", - [ASTRecordDeclInterface]> { + [ASTTagDeclInterface]> { let methods = [ InterfaceMethod<"", "bool", "isLambda", (ins), [{}], /*defaultImplementation=*/ [{ - return $_attr.getAstDecl()->isLambda(); + if (auto ast = clang::dyn_cast($_attr.getAstDecl())) + return ast->isLambda(); + return false; }] - > - ]; - } - - def ASTClassTemplateSpecializationDeclInterface : - AttrInterface<"ASTClassTemplateSpecializationDeclInterface", - [ASTCXXRecordDeclInterface]> { - let methods = [ - InterfaceMethod<"", "bool", "hasPromiseType", (ins), [{}], + >, + InterfaceMethod<"", "bool", "hasPromiseType", (ins), [{}], /*defaultImplementation=*/ [{ + if (!clang::isa($_attr.getAstDecl())) + return false; for (const auto *sub : $_attr.getAstDecl()->decls()) { if (auto subRec = clang::dyn_cast(sub)) { if (subRec->getDeclName().isIdentifier() && @@ -186,11 +177,6 @@ let cppNamespace = "::mlir::cir" in { ]; } - def ASTClassTemplatePartialSpecializationDeclInterface : - AttrInterface<"ASTClassTemplatePartialSpecializationDeclInterface", - [ASTClassTemplateSpecializationDeclInterface]>; - - def AnyASTFunctionDeclAttr : Attr< CPred<"::mlir::isa<::mlir::cir::ASTFunctionDeclInterface>($_self)">, "AST Function attribute"> { diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.h b/clang/lib/CIR/CodeGen/CIRGenBuilder.h index 4d2619cb89be..a52ad79a500e 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuilder.h +++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.h @@ -171,7 +171,7 @@ class CIRGenBuilderTy : public mlir::OpBuilder { structTy = getType( members, mlir::StringAttr::get(getContext()), /*body=*/true, packed, mlir::cir::StructType::Struct, - /*ast=*/mlir::Attribute()); + /*ast=*/std::nullopt); // Return zero or anonymous constant struct. if (isZero) @@ -413,23 +413,16 @@ class CIRGenBuilderTy : public mlir::OpBuilder { /// Get a CIR named struct type. mlir::cir::StructType getStructTy(llvm::ArrayRef members, llvm::StringRef name, bool body, - bool packed, mlir::Attribute ast) { + bool packed, const clang::RecordDecl *ast) { const auto nameAttr = getStringAttr(name); + std::optional astAttr = std::nullopt; auto kind = mlir::cir::StructType::RecordKind::Struct; - if (ast) - if (auto tagDecl = mlir::dyn_cast(ast)) - kind = getRecordKind(tagDecl.getTagKind()); + if (ast) { + astAttr = getAttr(ast); + kind = getRecordKind(ast->getTagKind()); + } return mlir::cir::StructType::get(getContext(), members, nameAttr, body, - packed, kind, ast); - } - - mlir::cir::StructType getStructTy(llvm::ArrayRef members, - llvm::StringRef name, bool body, - bool packed, const clang::RecordDecl *ast) { - mlir::Attribute astAttr; - if (ast) - astAttr = mlir::cir::makeAstDeclAttr(ast, getContext()); - return getStructTy(members, name, body, packed, astAttr); + packed, kind, astAttr); } // diff --git a/clang/lib/CIR/CodeGen/CIRGenTypes.cpp b/clang/lib/CIR/CodeGen/CIRGenTypes.cpp index e9a5fe9a4ee0..3ad25f3fbc76 100644 --- a/clang/lib/CIR/CodeGen/CIRGenTypes.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenTypes.cpp @@ -167,9 +167,7 @@ mlir::Type CIRGenTypes::convertRecordDeclType(const clang::RecordDecl *RD) { // Handle forward decl / incomplete types. if (!entry) { auto name = getRecordTypeName(RD, ""); - entry = - Builder.getStructTy({}, name, /*body=*/false, /*packed=*/false, - mlir::cir::makeAstDeclAttr(RD, &getMLIRContext())); + entry = Builder.getStructTy({}, name, /*body=*/false, /*packed=*/false, RD); recordDeclTypes[key] = entry; } diff --git a/clang/lib/CIR/CodeGen/CIRRecordLayoutBuilder.cpp b/clang/lib/CIR/CodeGen/CIRRecordLayoutBuilder.cpp index 935d0db3e502..f91e4b6a1af6 100644 --- a/clang/lib/CIR/CodeGen/CIRRecordLayoutBuilder.cpp +++ b/clang/lib/CIR/CodeGen/CIRRecordLayoutBuilder.cpp @@ -605,10 +605,8 @@ CIRGenTypes::computeRecordLayout(const RecordDecl *D, builder.astRecordLayout.getSize()) { CIRRecordLowering baseBuilder(*this, D, /*Packed=*/builder.isPacked); auto baseIdentifier = getRecordTypeName(D, ".base"); - *BaseTy = - Builder.getStructTy(baseBuilder.fieldTypes, baseIdentifier, - /*body=*/true, /*packed=*/false, - mlir::cir::makeAstDeclAttr(D, &getMLIRContext())); + *BaseTy = Builder.getStructTy(baseBuilder.fieldTypes, baseIdentifier, + /*body=*/true, /*packed=*/false, D); // TODO(cir): add something like addRecordTypeName // BaseTy and Ty must agree on their packedness for getCIRFieldNo to work @@ -622,8 +620,7 @@ CIRGenTypes::computeRecordLayout(const RecordDecl *D, // signifies that the type is no longer opaque and record layout is complete, // but we may need to recursively layout D while laying D out as a base type. *Ty = Builder.getStructTy(builder.fieldTypes, getRecordTypeName(D, ""), - /*body=*/true, /*packed=*/false, - mlir::cir::makeAstDeclAttr(D, &getMLIRContext())); + /*body=*/true, /*packed=*/false, D); auto RL = std::make_unique( Ty ? *Ty : mlir::cir::StructType{}, diff --git a/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp b/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp index d862fc65e74f..8d7b63d787e3 100644 --- a/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp @@ -49,38 +49,6 @@ using namespace mlir::cir; namespace mlir { namespace cir { -mlir::Attribute makeAstDeclAttr(const clang::Decl *decl, - mlir::MLIRContext *ctx) { - if (auto ast = clang::dyn_cast(decl)) - return ASTCXXConstructorDeclAttr::get(ctx, ast); - if (auto ast = clang::dyn_cast(decl)) - return ASTCXXConversionDeclAttr::get(ctx, ast); - if (auto ast = clang::dyn_cast(decl)) - return ASTCXXDestructorDeclAttr::get(ctx, ast); - if (auto ast = clang::dyn_cast(decl)) - return ASTCXXMethodDeclAttr::get(ctx, ast); - if (auto ast = clang::dyn_cast(decl)) - return ASTFunctionDeclAttr::get(ctx, ast); - if (auto ast = - clang::dyn_cast(decl)) - return ASTClassTemplatePartialSpecializationDeclAttr::get(ctx, ast); - if (auto ast = clang::dyn_cast(decl)) - return ASTClassTemplateSpecializationDeclAttr::get(ctx, ast); - if (auto ast = clang::dyn_cast(decl)) - return ASTCXXRecordDeclAttr::get(ctx, ast); - if (auto ast = clang::dyn_cast(decl)) - return ASTRecordDeclAttr::get(ctx, ast); - if (auto ast = clang::dyn_cast(decl)) - return ASTEnumDeclAttr::get(ctx, ast); - if (auto ast = clang::dyn_cast(decl)) - return ASTTagDeclAttr::get(ctx, ast); - if (auto ast = clang::dyn_cast(decl)) - return ASTTypeDeclAttr::get(ctx, ast); - if (auto ast = clang::dyn_cast(decl)) - return ASTVarDeclAttr::get(ctx, ast); - return ASTDeclAttr::get(ctx, decl); -}; - mlir::Attribute makeFuncDeclAttr(const clang::Decl *decl, mlir::MLIRContext *ctx) { return llvm::TypeSwitch(decl) diff --git a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp index a2a6803ee03e..4ea4ea482e2f 100644 --- a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp @@ -156,7 +156,7 @@ Type StructType::parse(mlir::AsmParser &parser) { return {}; return StructType::get(parser.getContext(), members, id, body, packed, kind, - mlir::Attribute()); + std::nullopt); } void StructType::print(mlir::AsmPrinter &printer) const { @@ -187,9 +187,9 @@ void StructType::print(mlir::AsmPrinter &printer) const { printer << "}"; } - if (getAst()) { + if (getAst().has_value()) { printer << " "; - printer.printAttribute(getAst()); + printer.printAttribute(getAst().value()); } printer << '>'; diff --git a/clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp b/clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp index 4a5ece6d8c06..44f271cacd2d 100644 --- a/clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp +++ b/clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp @@ -891,12 +891,7 @@ void LifetimeCheckPass::checkIf(IfOp ifOp) { template bool isStructAndHasAttr(mlir::Type ty) { if (!ty.isa()) return false; - auto sTy = ty.cast(); - auto recordDecl = sTy.getAst(); - if (auto interface = dyn_cast(recordDecl)) - if (hasAttr(interface)) - return true; - return false; + return hasAttr(*mlir::cast(ty).getAst()); } static bool isOwnerType(mlir::Type ty) { @@ -1762,8 +1757,7 @@ bool LifetimeCheckPass::isLambdaType(mlir::Type ty) { auto taskTy = ty.dyn_cast(); if (!taskTy) return false; - if (auto recordDecl = dyn_cast(taskTy.getAst())) - if (recordDecl.isLambda()) + if (taskTy.getAst()->isLambda()) IsLambdaTyCache[ty] = true; return IsLambdaTyCache[ty]; @@ -1778,12 +1772,8 @@ bool LifetimeCheckPass::isTaskType(mlir::Value taskVal) { auto taskTy = taskVal.getType().dyn_cast(); if (!taskTy) return false; - auto recordDecl = taskTy.getAst(); - auto spec = dyn_cast(recordDecl); - if (!spec) - return false; - return spec.hasPromiseType(); - } (); + return taskTy.getAst()->hasPromiseType(); + }(); IsTaskTyCache[ty] = result; return result; diff --git a/clang/test/CIR/CodeGen/bitfields.cpp b/clang/test/CIR/CodeGen/bitfields.cpp index 0cf8029b04d4..8f21b363c71e 100644 --- a/clang/test/CIR/CodeGen/bitfields.cpp +++ b/clang/test/CIR/CodeGen/bitfields.cpp @@ -14,5 +14,5 @@ void m() { __long l; } -// CHECK: !ty_22anon22 = !cir.struct +// CHECK: !ty_22anon22 = !cir.struct // CHECK: !ty_22__long22 = !cir.struct}> diff --git a/clang/test/CIR/CodeGen/dtors.cpp b/clang/test/CIR/CodeGen/dtors.cpp index d19bd90f5ac7..ae0d148d35ef 100644 --- a/clang/test/CIR/CodeGen/dtors.cpp +++ b/clang/test/CIR/CodeGen/dtors.cpp @@ -37,7 +37,7 @@ class B : public A }; // Class A -// CHECK: ![[ClassA:ty_.*]] = !cir.struct>>} #cir.cxxrecord.decl.ast> +// CHECK: ![[ClassA:ty_.*]] = !cir.struct>>} #cir.record.decl.ast> // Class B // CHECK: ![[ClassB:ty_.*]] = !cir.struct diff --git a/clang/test/CIR/CodeGen/struct.cpp b/clang/test/CIR/CodeGen/struct.cpp index 4ef84aa2ed48..07c5e7f70064 100644 --- a/clang/test/CIR/CodeGen/struct.cpp +++ b/clang/test/CIR/CodeGen/struct.cpp @@ -30,7 +30,7 @@ void yoyo(incomplete *i) {} // CHECK-DAG: !ty_22Bar22 = !cir.struct // CHECK-DAG: !ty_22Foo22 = !cir.struct -// CHECK-DAG: !ty_22Mandalore22 = !cir.struct, !s32i} #cir.cxxrecord.decl.ast> +// CHECK-DAG: !ty_22Mandalore22 = !cir.struct, !s32i} #cir.record.decl.ast> // CHECK-DAG: !ty_22Adv22 = !cir.struct // CHECK-DAG: !ty_22Entry22 = !cir.struct, !cir.ptr)>>}> diff --git a/clang/test/CIR/CodeGen/union.cpp b/clang/test/CIR/CodeGen/union.cpp index 58383a0e2442..f9b8db8ec158 100644 --- a/clang/test/CIR/CodeGen/union.cpp +++ b/clang/test/CIR/CodeGen/union.cpp @@ -6,10 +6,10 @@ typedef union { yolo y; struct { int lifecnt; }; } yolm; typedef union { yolo y; struct { int *lifecnt; int genpad; }; } yolm2; typedef union { yolo y; struct { bool life; int genpad; }; } yolm3; -// CHECK-DAG: !ty_22U23A3ADummy22 = !cir.struct -// CHECK-DAG: !ty_22anon221 = !cir.struct -// CHECK-DAG: !ty_22yolo22 = !cir.struct -// CHECK-DAG: !ty_22anon222 = !cir.struct, !s32i} #cir.cxxrecord.decl.ast> +// CHECK-DAG: !ty_22U23A3ADummy22 = !cir.struct +// CHECK-DAG: !ty_22anon221 = !cir.struct +// CHECK-DAG: !ty_22yolo22 = !cir.struct +// CHECK-DAG: !ty_22anon222 = !cir.struct, !s32i} #cir.record.decl.ast> // CHECK-DAG: !ty_22yolm22 = !cir.struct // CHECK-DAG: !ty_22yolm322 = !cir.struct @@ -33,14 +33,14 @@ union U2 { float f; } s; } u2; -// CHECK-DAG: !cir.struct +// CHECK-DAG: !cir.struct // Should genereate unions without padding. union U3 { short b; U u; } u3; -// CHECK-DAG: !ty_22U322 = !cir.struct +// CHECK-DAG: !ty_22U322 = !cir.struct void m() { yolm q; diff --git a/clang/test/CIR/CodeGen/vtable-rtti.cpp b/clang/test/CIR/CodeGen/vtable-rtti.cpp index 9830121c7848..6da37c786d2b 100644 --- a/clang/test/CIR/CodeGen/vtable-rtti.cpp +++ b/clang/test/CIR/CodeGen/vtable-rtti.cpp @@ -24,7 +24,7 @@ class B : public A // CHECK: ![[VTableTypeA:ty_.*]] = !cir.struct x 5>}> // Class A -// CHECK: ![[ClassA:ty_.*]] = !cir.struct>>} #cir.cxxrecord.decl.ast> +// CHECK: ![[ClassA:ty_.*]] = !cir.struct>>} #cir.record.decl.ast> // Class B // CHECK: ![[ClassB:ty_.*]] = !cir.struct diff --git a/clang/test/CIR/IR/global.cir b/clang/test/CIR/IR/global.cir index 47a862175f42..8ee44c5beeb0 100644 --- a/clang/test/CIR/IR/global.cir +++ b/clang/test/CIR/IR/global.cir @@ -3,7 +3,7 @@ !s8i = !cir.int !s32i = !cir.int !s64i = !cir.int -!ty_22Init22 = !cir.struct +!ty_22Init22 = !cir.struct module { cir.global external @a = #cir.int<3> : !s32i cir.global external @rgb = #cir.const_array<[#cir.int<0> : !s8i, #cir.int<-23> : !s8i, #cir.int<33> : !s8i] : !cir.array> diff --git a/clang/test/CIR/IR/invalid.cir b/clang/test/CIR/IR/invalid.cir index d3e653b967d8..ce7eafd6a1e8 100644 --- a/clang/test/CIR/IR/invalid.cir +++ b/clang/test/CIR/IR/invalid.cir @@ -488,7 +488,7 @@ module { // ----- !s8i = !cir.int -!ty_22Init22 = !cir.struct +!ty_22Init22 = !cir.struct module { cir.global "private" internal @_ZL8__ioinit = ctor : !ty_22Init22 { } @@ -498,7 +498,7 @@ module { // ----- !s8i = !cir.int #true = #cir.bool : !cir.bool -!ty_22Init22 = !cir.struct +!ty_22Init22 = !cir.struct module { cir.func private @_ZN4InitC1Eb(!cir.ptr) cir.global "private" internal @_ZL8__ioinit = ctor : !ty_22Init22 { diff --git a/clang/test/CIR/IR/struct.cir b/clang/test/CIR/IR/struct.cir index 0bee3430c60d..fb25d04533da 100644 --- a/clang/test/CIR/IR/struct.cir +++ b/clang/test/CIR/IR/struct.cir @@ -8,7 +8,7 @@ !ty_2222 = !cir.struct x 5>}> !ty_22221 = !cir.struct, !cir.ptr, !cir.ptr}> -!ty_22A22 = !cir.struct +!ty_22A22 = !cir.struct !ty_22i22 = !cir.struct !ty_22S22 = !cir.struct !ty_22S122 = !cir.struct diff --git a/clang/test/CIR/Lowering/array.cir b/clang/test/CIR/Lowering/array.cir index 2e92f54171ee..46ea393decd2 100644 --- a/clang/test/CIR/Lowering/array.cir +++ b/clang/test/CIR/Lowering/array.cir @@ -2,7 +2,7 @@ // RUN: cir-translate %s -cir-to-llvmir -o - | FileCheck %s -check-prefix=LLVM !s32i = !cir.int -!ty_22S22 = !cir.struct +!ty_22S22 = !cir.struct module { cir.func @foo() { diff --git a/clang/test/CIR/Lowering/struct.cir b/clang/test/CIR/Lowering/struct.cir index 2ef41da0a175..f68f96c0e72d 100644 --- a/clang/test/CIR/Lowering/struct.cir +++ b/clang/test/CIR/Lowering/struct.cir @@ -5,10 +5,10 @@ !u8i = !cir.int !u32i = !cir.int !ty_22S22 = !cir.struct -!ty_22S2A22 = !cir.struct -!ty_22S122 = !cir.struct} #cir.cxxrecord.decl.ast> -!ty_22S222 = !cir.struct -!ty_22S322 = !cir.struct +!ty_22S2A22 = !cir.struct +!ty_22S122 = !cir.struct} #cir.record.decl.ast> +!ty_22S222 = !cir.struct +!ty_22S322 = !cir.struct module { cir.func @test() { diff --git a/clang/test/CIR/Lowering/unions.cir b/clang/test/CIR/Lowering/unions.cir index c9c2b6518d4e..d2dbc4ca5f23 100644 --- a/clang/test/CIR/Lowering/unions.cir +++ b/clang/test/CIR/Lowering/unions.cir @@ -4,9 +4,9 @@ !s16i = !cir.int !s32i = !cir.int #true = #cir.bool : !cir.bool -!ty_22U122 = !cir.struct -!ty_22U222 = !cir.struct -!ty_22U322 = !cir.struct +!ty_22U122 = !cir.struct +!ty_22U222 = !cir.struct +!ty_22U322 = !cir.struct module { // Should lower union to struct with only the largest member. cir.global external @u1 = #cir.zero : !ty_22U122 From d022d2592fee4f5561ceaff208df33ddafbbc6ee Mon Sep 17 00:00:00 2001 From: Henrich Lauko Date: Fri, 15 Sep 2023 09:27:28 +0200 Subject: [PATCH 25/25] [CIR][IR] Remove duplicit optional of struct ast parameter. --- clang/include/clang/CIR/Dialect/IR/CIRTypes.td | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td index ddc8182da3b0..ea0738d19245 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td @@ -112,7 +112,7 @@ def CIR_StructType : CIR_Type<"Struct", "struct", "bool":$body, "bool":$packed, "mlir::cir::StructType::RecordKind":$kind, - OptionalParameter<"std::optional">:$ast + "std::optional":$ast ); let hasCustomAssemblyFormat = 1;