Skip to content

Commit

Permalink
[CPU] Move checking stack allocation cmd flag to Passes.cpp (#16062)
Browse files Browse the repository at this point in the history
This removes an additional flag, and puts all the flags at the same
file. It makes all the cmd flags start with `iree-llvmcpu-*`.
  • Loading branch information
hanhanW authored Jan 8, 2024
1 parent bd2c92d commit 6aa310c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ static llvm::cl::opt<int> clMaxAllocationSizeInBytes(
"iree-llvmcpu-stack-allocation-limit",
llvm::cl::desc("maximum allowed stack allocation size in bytes"),
llvm::cl::init(32768));
static llvm::cl::opt<bool> 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
Expand Down Expand Up @@ -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<func::FuncOp>()) {
if (clFailOnOutOfBoundsStackAllocation &&
failed(checkStackAllocationSize(funcOp))) {
if (failed(checkStackAllocationSize(funcOp))) {
return signalPassFailure();
}
}
}

std::unique_ptr<OperationPass<ModuleOp>>
createLLVMCPUCheckIRBeforeLLVMConversionPass() {
return std::make_unique<LLVMCPUCheckIRBeforeLLVMConversionPass>();
createLLVMCPUCheckIRBeforeLLVMConversionPass(bool failOnOutOfBounds) {
return std::make_unique<LLVMCPUCheckIRBeforeLLVMConversionPass>(
failOnOutOfBounds);
}

} // namespace mlir::iree_compiler
13 changes: 6 additions & 7 deletions compiler/src/iree/compiler/Codegen/LLVMCPU/Passes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> 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<bool> 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<bool> clCheckLinalgVectorization(
Expand Down Expand Up @@ -636,9 +636,8 @@ static void addLowerToLLVMPasses(OpPassManager &passManager,
passManager.addNestedPass<func::FuncOp>(createCleanupBufferAllocViewPass());

// Checking stack allocation before converting to CF dialect is easier.
if (clCheckIRBeforeLLVMConversion) {
passManager.addPass(createLLVMCPUCheckIRBeforeLLVMConversionPass());
}
passManager.addPass(createLLVMCPUCheckIRBeforeLLVMConversionPass(
clFailOnOutOfBoundsStackAllocation));

// SCF -> CF
passManager.addNestedPass<func::FuncOp>(createConvertSCFToCFPass());
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/iree/compiler/Codegen/LLVMCPU/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ createConvertToLLVMPass(bool reassociateFpReordering = false);

/// Checks CPU backend specific IR constraints (like no stack allocations)
std::unique_ptr<OperationPass<ModuleOp>>
createLLVMCPUCheckIRBeforeLLVMConversionPass();
createLLVMCPUCheckIRBeforeLLVMConversionPass(bool failOnOutOfBounds = true);

std::unique_ptr<OperationPass<func::FuncOp>>
createLLVMCPUEmitVectorizationRemarksPass();
Expand Down
5 changes: 5 additions & 0 deletions compiler/src/iree/compiler/Codegen/LLVMCPU/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -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 :
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -28,4 +28,4 @@ module {
return
}
}
// CHECK-LABEL: func @mix_static_and_unbound_dynamic_allocas(
// CHECK-LABEL: func @mix_static_and_unbound_dynamic_allocas(

0 comments on commit 6aa310c

Please sign in to comment.