From a3e92f760ffcf167ce5bfb333644b0955175f9c8 Mon Sep 17 00:00:00 2001 From: Vinicius Couto Espindola Date: Sat, 20 Jan 2024 07:16:09 -0300 Subject: [PATCH] Update base for Update on "[CIR][IR] Refactor for loops" This patch completes the deprecation of the generic `cir.loop` operation by adding a new `cir.for` operation and removing the `cir.loop` op. The new representation removes some bloat and places the regions in order of execution. [ghstack-poisoned] --- clang/include/clang/CIR/Dialect/IR/CIRDialect.h | 2 +- clang/include/clang/CIR/Dialect/IR/CIROps.td | 11 ++++++++--- .../{LoopOpInterface.h => CIRLoopOpInterface.h} | 10 +++++----- .../{LoopOpInterface.td => CIRLoopOpInterface.td} | 8 ++++---- clang/include/clang/CIR/Interfaces/CMakeLists.txt | 2 +- clang/lib/CIR/CodeGen/CMakeLists.txt | 1 + clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 2 +- clang/lib/CIR/Dialect/IR/CMakeLists.txt | 1 + clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp | 2 +- clang/lib/CIR/FrontendAction/CMakeLists.txt | 1 + .../{LoopOpInterface.cpp => CIRLoopOpInterface.cpp} | 10 ++++++---- clang/lib/CIR/Interfaces/CMakeLists.txt | 3 ++- clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt | 1 + clang/lib/CIR/Lowering/ThroughMLIR/CMakeLists.txt | 1 + 14 files changed, 34 insertions(+), 21 deletions(-) rename clang/include/clang/CIR/Interfaces/{LoopOpInterface.h => CIRLoopOpInterface.h} (77%) rename clang/include/clang/CIR/Interfaces/{LoopOpInterface.td => CIRLoopOpInterface.td} (93%) rename clang/lib/CIR/Interfaces/{LoopOpInterface.cpp => CIRLoopOpInterface.cpp} (83%) diff --git a/clang/include/clang/CIR/Dialect/IR/CIRDialect.h b/clang/include/clang/CIR/Dialect/IR/CIRDialect.h index 93a50cc8f3d6..f9bcf1c98311 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRDialect.h +++ b/clang/include/clang/CIR/Dialect/IR/CIRDialect.h @@ -33,7 +33,7 @@ #include "clang/CIR/Interfaces/ASTAttrInterfaces.h" #include "clang/CIR/Interfaces/CIROpInterfaces.h" -#include "clang/CIR/Interfaces/LoopOpInterface.h" +#include "clang/CIR/Interfaces/CIRLoopOpInterface.h" namespace mlir { namespace OpTrait { diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index 5b45e363c888..3f9f45478a21 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -20,7 +20,7 @@ include "clang/CIR/Dialect/IR/CIRAttrs.td" include "clang/CIR/Interfaces/ASTAttrInterfaces.td" include "clang/CIR/Interfaces/CIROpInterfaces.td" -include "clang/CIR/Interfaces/LoopOpInterface.td" +include "clang/CIR/Interfaces/CIRLoopOpInterface.td" include "mlir/Interfaces/ControlFlowInterfaces.td" include "mlir/Interfaces/FunctionInterfaces.td" @@ -1222,9 +1222,14 @@ def LoopOp : CIR_Op<"loop", } llvm::SmallVector getRegionsInExecutionOrder() { - if (getKind() == LoopOpKind::For) + switch(getKind()) { + case LoopOpKind::For: return llvm::SmallVector{&getCond(), &getBody(), &getStep()}; - return llvm::SmallVector{&getCond(), &getBody()}; + case LoopOpKind::While: + return llvm::SmallVector{&getCond(), &getBody()}; + case LoopOpKind::DoWhile: + return llvm::SmallVector{&getBody(), &getCond()}; + } } }]; } diff --git a/clang/include/clang/CIR/Interfaces/LoopOpInterface.h b/clang/include/clang/CIR/Interfaces/CIRLoopOpInterface.h similarity index 77% rename from clang/include/clang/CIR/Interfaces/LoopOpInterface.h rename to clang/include/clang/CIR/Interfaces/CIRLoopOpInterface.h index 96198c635aa9..2e8a0c8e8a94 100644 --- a/clang/include/clang/CIR/Interfaces/LoopOpInterface.h +++ b/clang/include/clang/CIR/Interfaces/CIRLoopOpInterface.h @@ -1,4 +1,4 @@ -//===- LoopOpInterface.h - Interface for CIR loop-like ops -----*- C++ -*-===// +//===- CIRLoopOpInterface.h - Interface for CIR loop-like ops --*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -10,8 +10,8 @@ // //===----------------------------------------------------------------------===// -#ifndef CLANG_INTERFACES_CIR_LOOPOPINTERFACE_H_ -#define CLANG_INTERFACES_CIR_LOOPOPINTERFACE_H_ +#ifndef CLANG_INTERFACES_CIR_CIRLOOPOPINTERFACE_H_ +#define CLANG_INTERFACES_CIR_CIRLOOPOPINTERFACE_H_ #include "mlir/IR/BuiltinTypes.h" #include "mlir/IR/OpDefinition.h" @@ -31,6 +31,6 @@ ::mlir::LogicalResult verifyLoopOpInterface(::mlir::Operation *op); } // namespace mlir /// Include the tablegen'd interface declarations. -#include "clang/CIR/Interfaces/LoopOpInterface.h.inc" +#include "clang/CIR/Interfaces/CIRLoopOpInterface.h.inc" -#endif // CLANG_INTERFACES_CIR_LOOPOPINTERFACE_H_ +#endif // CLANG_INTERFACES_CIR_CIRLOOPOPINTERFACE_H_ diff --git a/clang/include/clang/CIR/Interfaces/LoopOpInterface.td b/clang/include/clang/CIR/Interfaces/CIRLoopOpInterface.td similarity index 93% rename from clang/include/clang/CIR/Interfaces/LoopOpInterface.td rename to clang/include/clang/CIR/Interfaces/CIRLoopOpInterface.td index ada6f8f86c5b..c2b871785ffd 100644 --- a/clang/include/clang/CIR/Interfaces/LoopOpInterface.td +++ b/clang/include/clang/CIR/Interfaces/CIRLoopOpInterface.td @@ -1,4 +1,4 @@ -//===- LoopOpInterface.td - Interface for CIR loop-like ops ----*- C++ -*-===// +//===- CIRLoopOpInterface.td - Interface for CIR loop-like ops -*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -6,8 +6,8 @@ // //===---------------------------------------------------------------------===// -#ifndef CLANG_CIR_INTERFACES_LOOPOPINTERFACE -#define CLANG_CIR_INTERFACES_LOOPOPINTERFACE +#ifndef CLANG_CIR_INTERFACES_CIRLOOPOPINTERFACE +#define CLANG_CIR_INTERFACES_CIRLOOPOPINTERFACE include "mlir/IR/OpBase.td" include "mlir/Interfaces/ControlFlowInterfaces.td" @@ -97,4 +97,4 @@ def LoopOpInterface : OpInterface<"LoopOpInterface", [ }]; } -#endif // CLANG_CIR_INTERFACES_LOOPOPINTERFACE +#endif // CLANG_CIR_INTERFACES_CIRLOOPOPINTERFACE diff --git a/clang/include/clang/CIR/Interfaces/CMakeLists.txt b/clang/include/clang/CIR/Interfaces/CMakeLists.txt index 383eda4311a3..c7132abca833 100644 --- a/clang/include/clang/CIR/Interfaces/CMakeLists.txt +++ b/clang/include/clang/CIR/Interfaces/CMakeLists.txt @@ -22,4 +22,4 @@ endfunction() add_clang_mlir_attr_interface(ASTAttrInterfaces) add_clang_mlir_op_interface(CIROpInterfaces) -add_clang_mlir_op_interface(LoopOpInterface) +add_clang_mlir_op_interface(CIRLoopOpInterface) diff --git a/clang/lib/CIR/CodeGen/CMakeLists.txt b/clang/lib/CIR/CodeGen/CMakeLists.txt index 62df7a8d3d68..5af8f15aac34 100644 --- a/clang/lib/CIR/CodeGen/CMakeLists.txt +++ b/clang/lib/CIR/CodeGen/CMakeLists.txt @@ -41,6 +41,7 @@ add_clang_library(clangCIR MLIRCIROpsIncGen MLIRCIRASTAttrInterfacesIncGen MLIRCIROpInterfacesIncGen + MLIRCIRLoopOpInterfaceIncGen ${dialect_libs} LINK_LIBS diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp index e2c71c1b2bcc..b5a8bfd2f698 100644 --- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp @@ -14,7 +14,7 @@ #include "clang/CIR/Dialect/IR/CIRAttrs.h" #include "clang/CIR/Dialect/IR/CIROpsEnums.h" #include "clang/CIR/Dialect/IR/CIRTypes.h" -#include "clang/CIR/Interfaces/LoopOpInterface.h" +#include "clang/CIR/Interfaces/CIRLoopOpInterface.h" #include "llvm/Support/ErrorHandling.h" #include diff --git a/clang/lib/CIR/Dialect/IR/CMakeLists.txt b/clang/lib/CIR/Dialect/IR/CMakeLists.txt index b8cc5b84e93e..f4609c3aad32 100644 --- a/clang/lib/CIR/Dialect/IR/CMakeLists.txt +++ b/clang/lib/CIR/Dialect/IR/CMakeLists.txt @@ -11,6 +11,7 @@ add_clang_library(MLIRCIR MLIRSymbolInterfacesIncGen MLIRCIRASTAttrInterfacesIncGen MLIRCIROpInterfacesIncGen + MLIRCIRLoopOpInterfaceIncGen LINK_LIBS PUBLIC MLIRIR diff --git a/clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp b/clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp index 6a08f24466e6..e77a6bdf14b8 100644 --- a/clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp +++ b/clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp @@ -16,7 +16,7 @@ #include "clang/CIR/Dialect/IR/CIRDialect.h" #include "clang/CIR/Dialect/Passes.h" -#include "clang/CIR/Interfaces/LoopOpInterface.h" +#include "clang/CIR/Interfaces/CIRLoopOpInterface.h" #include "llvm/ADT/SetOperations.h" #include "llvm/ADT/SmallSet.h" diff --git a/clang/lib/CIR/FrontendAction/CMakeLists.txt b/clang/lib/CIR/FrontendAction/CMakeLists.txt index 31ca49fedf44..077bd733cbd8 100644 --- a/clang/lib/CIR/FrontendAction/CMakeLists.txt +++ b/clang/lib/CIR/FrontendAction/CMakeLists.txt @@ -12,6 +12,7 @@ add_clang_library(clangCIRFrontendAction MLIRCIROpsIncGen MLIRCIRASTAttrInterfacesIncGen MLIRCIROpInterfacesIncGen + MLIRCIRLoopOpInterfaceIncGen MLIRBuiltinLocationAttributesIncGen MLIRBuiltinTypeInterfacesIncGen MLIRFunctionInterfacesIncGen diff --git a/clang/lib/CIR/Interfaces/LoopOpInterface.cpp b/clang/lib/CIR/Interfaces/CIRLoopOpInterface.cpp similarity index 83% rename from clang/lib/CIR/Interfaces/LoopOpInterface.cpp rename to clang/lib/CIR/Interfaces/CIRLoopOpInterface.cpp index 7a481a82d857..ae5947a56944 100644 --- a/clang/lib/CIR/Interfaces/LoopOpInterface.cpp +++ b/clang/lib/CIR/Interfaces/CIRLoopOpInterface.cpp @@ -1,4 +1,4 @@ -//===- LoopOpInterface.cpp - Interface for CIR loop-like ops ---*- C++ -*-===// +//===- CIRLoopOpInterface.cpp - Interface for CIR loop-like ops *- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -6,10 +6,11 @@ // //===---------------------------------------------------------------------===// -#include "clang/CIR/Interfaces/LoopOpInterface.h" +#include "clang/CIR/Interfaces/CIRLoopOpInterface.h" #include "clang/CIR/Dialect/IR/CIRDialect.h" -#include "clang/CIR/Interfaces/LoopOpInterface.cpp.inc" +#include "clang/CIR/Interfaces/CIRLoopOpInterface.cpp.inc" +#include "llvm/Support/ErrorHandling.h" namespace mlir { namespace cir { @@ -28,8 +29,9 @@ void LoopOpInterface::getLoopOpSuccessorRegions( regions.emplace_back(RegionSuccessor(op->getResults())); regions.emplace_back(&op.getBody(), op.getBody().getArguments()); } - // Branching from body: go to step (for) or condition (while). + // Branching from body: go to step (for) or condition. else if (&op.getBody() == point.getRegionOrNull()) { + // FIXME(cir): Should we consider break/continue statements here? auto *afterBody = (op.maybeGetStep() ? op.maybeGetStep() : &op.getCond()); regions.emplace_back(afterBody, afterBody->getArguments()); } diff --git a/clang/lib/CIR/Interfaces/CMakeLists.txt b/clang/lib/CIR/Interfaces/CMakeLists.txt index 74d2c09ad61e..84322f4836e0 100644 --- a/clang/lib/CIR/Interfaces/CMakeLists.txt +++ b/clang/lib/CIR/Interfaces/CMakeLists.txt @@ -1,7 +1,7 @@ add_clang_library(MLIRCIRInterfaces ASTAttrInterfaces.cpp CIROpInterfaces.cpp - LoopOpInterface.cpp + CIRLoopOpInterface.cpp ADDITIONAL_HEADER_DIRS ${MLIR_MAIN_INCLUDE_DIR}/mlir/Interfaces @@ -9,6 +9,7 @@ add_clang_library(MLIRCIRInterfaces DEPENDS MLIRCIRASTAttrInterfacesIncGen MLIRCIROpInterfacesIncGen + MLIRCIRLoopOpInterfaceIncGen LINK_LIBS ${dialect_libs} diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt b/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt index 14b879ee1c44..f613972126c8 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt +++ b/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt @@ -14,6 +14,7 @@ add_clang_library(clangCIRLoweringDirectToLLVM MLIRCIROpsIncGen MLIRCIRASTAttrInterfacesIncGen MLIRCIROpInterfacesIncGen + MLIRCIRLoopOpInterfaceIncGen MLIRBuiltinLocationAttributesIncGen MLIRBuiltinTypeInterfacesIncGen MLIRFunctionInterfacesIncGen diff --git a/clang/lib/CIR/Lowering/ThroughMLIR/CMakeLists.txt b/clang/lib/CIR/Lowering/ThroughMLIR/CMakeLists.txt index d4a945ab7915..3ae144024ae7 100644 --- a/clang/lib/CIR/Lowering/ThroughMLIR/CMakeLists.txt +++ b/clang/lib/CIR/Lowering/ThroughMLIR/CMakeLists.txt @@ -14,6 +14,7 @@ add_clang_library(clangCIRLoweringThroughMLIR MLIRCIREnumsGen MLIRCIRASTAttrInterfacesIncGen MLIRCIROpInterfacesIncGen + MLIRCIRLoopOpInterfaceIncGen MLIRBuiltinLocationAttributesIncGen MLIRBuiltinTypeInterfacesIncGen MLIRFunctionInterfacesIncGen