Skip to content

Commit

Permalink
Enable checking stack allocation before converting to LLVM dialect. (#…
Browse files Browse the repository at this point in the history
…8976)

The flag is not removed because sometimes we might want to bypass the check.
  • Loading branch information
hanhanW authored Apr 27, 2022
1 parent 267fadb commit 05e0db5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,25 @@ struct LLVMCPUCheckIRBeforeLLVMConversionPass

void LLVMCPUCheckIRBeforeLLVMConversionPass::runOnOperation() {
auto moduleOp = getOperation();
// For now only check that there are no stack allocations.
auto walkResult = moduleOp.walk([](memref::AllocaOp allocaOp) -> WalkResult {
return allocaOp.emitOpError("expected no static allocations");
int64_t bits = 0;
auto walkResult = moduleOp.walk([&](memref::AllocaOp allocaOp) -> WalkResult {
auto type = allocaOp.getType().cast<ShapedType>();
if (!type.hasStaticShape()) {
return allocaOp.emitOpError(
"expected no stack allocations with dynamic shapes");
}
bits += type.getSizeInBits();
return WalkResult::advance();
});
if (walkResult.wasInterrupted()) {
return signalPassFailure();
}
constexpr int k16KBInBits = 16 * 1024 * 8;
if (bits >= k16KBInBits) {
moduleOp.emitOpError(
"expected total size of stack allocation is smaller than 16 KB");
return signalPassFailure();
}
}

std::unique_ptr<OperationPass<ModuleOp>>
Expand Down
2 changes: 1 addition & 1 deletion iree/compiler/Codegen/LLVMCPU/Passes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ 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"),
llvm::cl::init(false));
llvm::cl::init(true));

//===---------------------------------------------------------------------===//
// Default allocation functions for CPU backend
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
// RUN: iree-opt -iree-llvmcpu-check-ir-before-llvm-conversion %s -verify-diagnostics -split-input-file

func.func @no_static_allocas(%arg0: index) {
// expected-error @+1 {{expected no static allocations}}
module {
func.func @no_dynamic_allocas(%arg0: index) {
// expected-error @+1 {{expected no stack allocations with dynamic shapes}}
%0 = memref.alloca(%arg0) : memref<?xf32>
return
}
}

// -----

// expected-error @+1 {{expected total size of stack allocation is smaller than 16 KB}}
module {
func.func @big_allocas(%arg0: index) {
%0 = memref.alloca() : memref<65536xi32>
return
}
}

0 comments on commit 05e0db5

Please sign in to comment.