From 6aa310c8849855d5767cbf809aa2e81630864cf8 Mon Sep 17 00:00:00 2001 From: Han-Chung Wang Date: Mon, 8 Jan 2024 13:42:29 -0800 Subject: [PATCH] [CPU] Move checking stack allocation cmd flag to Passes.cpp (#16062) This removes an additional flag, and puts all the flags at the same file. It makes all the cmd flags start with `iree-llvmcpu-*`. --- .../LLVMCPUCheckIRBeforeLLVMConversion.cpp | 22 ++++++++++--------- .../iree/compiler/Codegen/LLVMCPU/Passes.cpp | 13 +++++------ .../iree/compiler/Codegen/LLVMCPU/Passes.h | 2 +- .../iree/compiler/Codegen/LLVMCPU/Passes.td | 5 +++++ ...fore_llvm_conversion_not_fail_unbound.mlir | 4 ++-- 5 files changed, 26 insertions(+), 20 deletions(-) diff --git a/compiler/src/iree/compiler/Codegen/LLVMCPU/LLVMCPUCheckIRBeforeLLVMConversion.cpp b/compiler/src/iree/compiler/Codegen/LLVMCPU/LLVMCPUCheckIRBeforeLLVMConversion.cpp index 5fdc12067000..5b9c72e771ea 100644 --- a/compiler/src/iree/compiler/Codegen/LLVMCPU/LLVMCPUCheckIRBeforeLLVMConversion.cpp +++ b/compiler/src/iree/compiler/Codegen/LLVMCPU/LLVMCPUCheckIRBeforeLLVMConversion.cpp @@ -16,16 +16,15 @@ static llvm::cl::opt clMaxAllocationSizeInBytes( "iree-llvmcpu-stack-allocation-limit", llvm::cl::desc("maximum allowed stack allocation size in bytes"), llvm::cl::init(32768)); -static llvm::cl::opt clFailOnOutOfBoundsStackAllocation( - "iree-llvmcpu-fail-on-out-of-bounds-stack-allocation", - llvm::cl::desc("fail if the upper bound of dynamic stack allocation cannot " - "be solved"), - llvm::cl::init(true)); namespace { struct LLVMCPUCheckIRBeforeLLVMConversionPass : LLVMCPUCheckIRBeforeLLVMConversionBase< LLVMCPUCheckIRBeforeLLVMConversionPass> { + LLVMCPUCheckIRBeforeLLVMConversionPass(bool failOnOutOfBounds) { + this->failOnOutOfBounds = failOnOutOfBounds; + } + void runOnOperation() override; }; } // namespace @@ -84,19 +83,22 @@ static LogicalResult checkStackAllocationSize(func::FuncOp funcOp) { } void LLVMCPUCheckIRBeforeLLVMConversionPass::runOnOperation() { - auto moduleOp = getOperation(); + if (!failOnOutOfBounds) { + return; + } + auto moduleOp = getOperation(); for (auto funcOp : moduleOp.getOps()) { - if (clFailOnOutOfBoundsStackAllocation && - failed(checkStackAllocationSize(funcOp))) { + if (failed(checkStackAllocationSize(funcOp))) { return signalPassFailure(); } } } std::unique_ptr> -createLLVMCPUCheckIRBeforeLLVMConversionPass() { - return std::make_unique(); +createLLVMCPUCheckIRBeforeLLVMConversionPass(bool failOnOutOfBounds) { + return std::make_unique( + failOnOutOfBounds); } } // namespace mlir::iree_compiler diff --git a/compiler/src/iree/compiler/Codegen/LLVMCPU/Passes.cpp b/compiler/src/iree/compiler/Codegen/LLVMCPU/Passes.cpp index b0fbf23e2cca..4f876ba526be 100644 --- a/compiler/src/iree/compiler/Codegen/LLVMCPU/Passes.cpp +++ b/compiler/src/iree/compiler/Codegen/LLVMCPU/Passes.cpp @@ -31,10 +31,10 @@ namespace mlir::iree_compiler { /// Command line options used purely for development purposes. Not to be relied /// on in any way. -static llvm::cl::opt clCheckIRBeforeLLVMConversion( - "iree-codegen-check-ir-before-llvm-conversion", - llvm::cl::desc("Runs the pass to check the IR generated from LLVMCPU " - "before conversion to LLVM IR"), +static llvm::cl::opt clFailOnOutOfBoundsStackAllocation( + "iree-llvmcpu-fail-on-out-of-bounds-stack-allocation", + llvm::cl::desc("fail if the upper bound of dynamic stack allocation cannot " + "be solved"), llvm::cl::init(true)); static llvm::cl::opt clCheckLinalgVectorization( @@ -636,9 +636,8 @@ static void addLowerToLLVMPasses(OpPassManager &passManager, passManager.addNestedPass(createCleanupBufferAllocViewPass()); // Checking stack allocation before converting to CF dialect is easier. - if (clCheckIRBeforeLLVMConversion) { - passManager.addPass(createLLVMCPUCheckIRBeforeLLVMConversionPass()); - } + passManager.addPass(createLLVMCPUCheckIRBeforeLLVMConversionPass( + clFailOnOutOfBoundsStackAllocation)); // SCF -> CF passManager.addNestedPass(createConvertSCFToCFPass()); diff --git a/compiler/src/iree/compiler/Codegen/LLVMCPU/Passes.h b/compiler/src/iree/compiler/Codegen/LLVMCPU/Passes.h index f4d0b300e9bf..74d688d2531a 100644 --- a/compiler/src/iree/compiler/Codegen/LLVMCPU/Passes.h +++ b/compiler/src/iree/compiler/Codegen/LLVMCPU/Passes.h @@ -25,7 +25,7 @@ createConvertToLLVMPass(bool reassociateFpReordering = false); /// Checks CPU backend specific IR constraints (like no stack allocations) std::unique_ptr> -createLLVMCPUCheckIRBeforeLLVMConversionPass(); +createLLVMCPUCheckIRBeforeLLVMConversionPass(bool failOnOutOfBounds = true); std::unique_ptr> createLLVMCPUEmitVectorizationRemarksPass(); diff --git a/compiler/src/iree/compiler/Codegen/LLVMCPU/Passes.td b/compiler/src/iree/compiler/Codegen/LLVMCPU/Passes.td index 8812745b2a9c..73e57ddc2b3c 100644 --- a/compiler/src/iree/compiler/Codegen/LLVMCPU/Passes.td +++ b/compiler/src/iree/compiler/Codegen/LLVMCPU/Passes.td @@ -49,6 +49,11 @@ def LLVMCPUCheckIRBeforeLLVMConversion : Pass<"iree-llvmcpu-check-ir-before-llvm-conversion", "ModuleOp"> { let summary = "Checks CPU backend specific IR constraints (like no allocas)"; let constructor = "mlir::iree_compiler::createLLVMCPUCheckIRBeforeLLVMConversionPass()"; + let options = [ + Option<"failOnOutOfBounds", "fail-on-out-of-bounds", "bool", "true", + "Fails if the upper bound of dynamic stack allocation cannot be" + "resolved or is more than the limit."> + ]; } def LLVMCPUEmitVectorizationRemarks : diff --git a/compiler/src/iree/compiler/Codegen/LLVMCPU/test/check_ir_before_llvm_conversion_not_fail_unbound.mlir b/compiler/src/iree/compiler/Codegen/LLVMCPU/test/check_ir_before_llvm_conversion_not_fail_unbound.mlir index adae2c33b683..33488931bb5d 100644 --- a/compiler/src/iree/compiler/Codegen/LLVMCPU/test/check_ir_before_llvm_conversion_not_fail_unbound.mlir +++ b/compiler/src/iree/compiler/Codegen/LLVMCPU/test/check_ir_before_llvm_conversion_not_fail_unbound.mlir @@ -1,4 +1,4 @@ -// RUN: iree-opt --iree-llvmcpu-check-ir-before-llvm-conversion --iree-llvmcpu-fail-on-out-of-bounds-stack-allocation=false %s --verify-diagnostics -split-input-file +// RUN: iree-opt --iree-llvmcpu-check-ir-before-llvm-conversion=fail-on-out-of-bounds=false %s --verify-diagnostics -split-input-file module { func.func @dynamic_allocas(%arg0: index) { @@ -28,4 +28,4 @@ module { return } } -// CHECK-LABEL: func @mix_static_and_unbound_dynamic_allocas( \ No newline at end of file +// CHECK-LABEL: func @mix_static_and_unbound_dynamic_allocas(