Skip to content
forked from iree-org/iree

Commit 43850fc

Browse files
authored
fix(iree): adjust stack allocation patches per upstream review (#37)
- Address formatting issues - Move the LIT test, register with Bazel & CMake - Use `PowerOf2ByteSize` for the CLI option, add negative LIT check - Improve readability in the verification pass
1 parent f39b1f1 commit 43850fc

File tree

6 files changed

+25
-10
lines changed

6 files changed

+25
-10
lines changed

Diff for: compiler/plugins/target/LLVMCPU/LLVMTargetOptions.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ void LLVMCPUTargetCLOptions::bindOptions(OptionsBinder &binder) {
582582
targetVectorWidthInBytes, llvm::cl::cat(category),
583583
llvm::cl::desc("Overrides the native vector register "
584584
"width (in bytes) of the target."));
585-
binder.opt<unsigned>(
585+
binder.opt<llvm::cl::PowerOf2ByteSize>(
586586
"iree-llvmcpu-stack-allocation-limit", targetMaxStackAllocSizeInBytes,
587587
llvm::cl::cat(category),
588588
llvm::cl::desc(
@@ -640,7 +640,7 @@ LLVMTargetOptions LLVMCPUTargetCLOptions::getTargetOptions() {
640640
target.llvmTargetOptions.FloatABIType = targetFloatABI;
641641
target.dataLayout = targetDataLayout;
642642
target.vectorWidthInBytes = targetVectorWidthInBytes;
643-
target.maxStackAllocSizeInBytes = targetMaxStackAllocSizeInBytes;
643+
target.maxStackAllocSizeInBytes = targetMaxStackAllocSizeInBytes.value;
644644
target.ukernels = enableUkernels;
645645
target.linkUkernelBitcode = linkUKernelBitcode;
646646

Diff for: compiler/plugins/target/LLVMCPU/LLVMTargetOptions.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ enum class SanitizerKind {
3333
struct LLVMTarget {
3434
static constexpr const char *DEFAULT_DATA_LAYOUT = "";
3535
static constexpr int64_t DEFAULT_VECTOR_WIDTH_IN_BYTES = 0;
36-
static constexpr unsigned DEFAULT_MAX_STACK_ALLOC_SIZE_IN_BYTES = 32768;
36+
static constexpr int64_t DEFAULT_MAX_STACK_ALLOC_SIZE_IN_BYTES = 32768;
3737
static constexpr bool DEFAULT_LINK_EMBEDDED = true;
3838
static constexpr bool DEFAULT_DEBUG_SYMBOLS = true;
3939
static constexpr SanitizerKind DEFAULT_SANITIZER_KIND = SanitizerKind::kNone;
@@ -89,7 +89,7 @@ struct LLVMTarget {
8989
std::string dataLayout = DEFAULT_DATA_LAYOUT;
9090
// Overrides the vector width (in bytes) of the target.
9191
int64_t vectorWidthInBytes = DEFAULT_VECTOR_WIDTH_IN_BYTES;
92-
unsigned maxStackAllocSizeInBytes = DEFAULT_MAX_STACK_ALLOC_SIZE_IN_BYTES;
92+
int64_t maxStackAllocSizeInBytes = DEFAULT_MAX_STACK_ALLOC_SIZE_IN_BYTES;
9393

9494
llvm::PipelineTuningOptions pipelineTuningOptions;
9595
// Optimization level to be used by the LLVM optimizer (middle-end).
@@ -196,7 +196,7 @@ struct LLVMCPUTargetCLOptions {
196196
llvm::FloatABI::ABIType targetFloatABI = LLVMTarget::DEFAULT_FLOAT_ABI;
197197
std::string targetDataLayout = LLVMTarget::DEFAULT_DATA_LAYOUT;
198198
unsigned targetVectorWidthInBytes = LLVMTarget::DEFAULT_VECTOR_WIDTH_IN_BYTES;
199-
unsigned targetMaxStackAllocSizeInBytes =
199+
llvm::cl::PowerOf2ByteSize targetMaxStackAllocSizeInBytes =
200200
LLVMTarget::DEFAULT_MAX_STACK_ALLOC_SIZE_IN_BYTES;
201201
std::string enableUkernels = LLVMTarget::DEFAULT_ENABLE_UKERNELS;
202202
bool linkUKernelBitcode = LLVMTarget::DEFAULT_LINK_UKERNEL_BITCODE;

Diff for: compiler/plugins/target/LLVMCPU/test/BUILD.bazel

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ iree_lit_test_suite(
1616
name = "lit",
1717
srcs = enforce_glob(
1818
[
19+
"hal_target_device_attributes.mlir",
1920
"materialize_homogeneous_encodings.mlir",
2021
"smoketest_embedded.mlir",
2122
"smoketest_system.mlir",
@@ -24,8 +25,10 @@ iree_lit_test_suite(
2425
),
2526
cfg = "//compiler:lit.cfg.py",
2627
tools = [
28+
"//tools:iree-compile",
2729
"//tools:iree-opt",
2830
"@llvm-project//lld",
2931
"@llvm-project//llvm:FileCheck",
32+
"@llvm-project//llvm:not",
3033
],
3134
)

Diff for: compiler/plugins/target/LLVMCPU/test/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ iree_lit_test_suite(
1414
NAME
1515
lit
1616
SRCS
17+
"hal_target_device_attributes.mlir"
1718
"materialize_homogeneous_encodings.mlir"
1819
"smoketest_embedded.mlir"
1920
"smoketest_system.mlir"
2021
TOOLS
2122
${IREE_LLD_TARGET}
2223
FileCheck
24+
iree-compile
2325
iree-opt
26+
not
2427
)
2528

2629
### BAZEL_TO_CMAKE_PRESERVES_ALL_CONTENT_BELOW_THIS_LINE ###

Diff for: compiler/src/iree/compiler/Codegen/LLVMCPU/test/hal_target_device_attributes.mlir renamed to compiler/plugins/target/LLVMCPU/test/hal_target_device_attributes.mlir

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
//
2727
// CHECK-STACK-VALUE-SAME: }>]> : !hal.device
2828

29+
// RUN: not iree-compile --compile-to=preprocessing --iree-hal-target-backends=llvm-cpu --iree-llvmcpu-target-triple=x86_64-linux-gnu %s \
30+
// RUN: --iree-llvmcpu-stack-allocation-limit=64266 \
31+
// RUN: 2>&1 | FileCheck %s --check-prefix=CHECK-INCORRECT-OPT-STACK-VALUE
32+
//
33+
// CHECK-INCORRECT-OPT-STACK-VALUE: for the --iree-llvmcpu-stack-allocation-limit option: '64266' value not a power-of-two
34+
2935
module {
3036
util.func public @foo(%arg0: tensor<?xf32>) -> tensor<?xf32> {
3137
util.return %arg0 : tensor<?xf32>

Diff for: compiler/src/iree/compiler/Codegen/LLVMCPU/LLVMCPUCheckIRBeforeLLVMConversion.cpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,17 @@ struct LLVMCPUCheckIRBeforeLLVMConversionPass
3434
};
3535
} // namespace
3636

37-
/// Returns success if the cummulative stack allocation size is less than the
38-
/// limit set by clMaxAllocationSizeInBytes.
37+
/// Returns success if the cumulative stack allocation size is less than the
38+
/// limit set through --iree-llvmcpu-stack-allocation-limit (or the default
39+
/// defined for HAL LLVMCPU target).
3940
static LogicalResult
4041
checkStackAllocationSize(mlir::FunctionOpInterface funcOp) {
4142
if (funcOp.getFunctionBody().empty())
4243
return success();
4344

44-
unsigned maxAllocationSizeInBytes = 32768;
45+
// In rare cases where the attribute is not present in the module, a value of
46+
// 32KB will be taken.
47+
unsigned maxAllocationSizeInBytes = 32 * 1024;
4548
auto targetAttr = IREE::HAL::ExecutableTargetAttr::lookup(funcOp);
4649
if (targetAttr) {
4750
auto nativeAllocationSizeAttr =
@@ -99,8 +102,8 @@ checkStackAllocationSize(mlir::FunctionOpInterface funcOp) {
99102
}
100103
if (cumSize > maxAllocationSizeInBytes) {
101104
return funcOp.emitOpError("exceeded stack allocation limit of ")
102-
<< maxAllocationSizeInBytes << " bytes for function. Got "
103-
<< cumSize << " bytes";
105+
<< maxAllocationSizeInBytes << " bytes for function. Got " << cumSize
106+
<< " bytes";
104107
}
105108
return success();
106109
}

0 commit comments

Comments
 (0)