Skip to content

Commit c361435

Browse files
committed
[mlir][StandardToSPIRV] Handle i1 case for lowering memref.load/store op
This patch unconditionally converts i1 types to i8 types on memrefs. If the extensions or capabilities are not met, they will be converted to i32. Hence the logic in IntLoadPattern and IntStorePattern are also updated. Also added the implementation of SPIRVTypeConverter::getOptions(). Reviewed By: antiagainst Differential Revision: https://reviews.llvm.org/D99724
1 parent 04e9cd0 commit c361435

5 files changed

Lines changed: 152 additions & 18 deletions

File tree

‎mlir/include/mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,13 @@ class SPIRVTypeConverter : public TypeConverter {
4949
/// values will be packed into one 32-bit value to be memory efficient.
5050
bool emulateNon32BitScalarTypes;
5151

52+
/// The number of bits to store a boolean value. It is eight bits by
53+
/// default.
54+
unsigned boolNumBits;
55+
5256
// Note: we need this instead of inline initializers becuase of
5357
// https://bugs.llvm.org/show_bug.cgi?id=36684
54-
Options() : emulateNon32BitScalarTypes(true) {}
58+
Options() : emulateNon32BitScalarTypes(true), boolNumBits(8) {}
5559
};
5660

5761
explicit SPIRVTypeConverter(spirv::TargetEnvAttr targetAttr,

‎mlir/lib/Conversion/StandardToSPIRV/StandardToSPIRV.cpp‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,9 @@ IntLoadOpPattern::matchAndRewrite(memref::LoadOp loadOp,
991991
loadOperands.indices(), loc, rewriter);
992992

993993
int srcBits = memrefType.getElementType().getIntOrFloatBitWidth();
994+
bool isBool = srcBits == 1;
995+
if (isBool)
996+
srcBits = typeConverter.getOptions().boolNumBits;
994997
auto dstType = typeConverter.convertType(memrefType)
995998
.cast<spirv::PointerType>()
996999
.getPointeeType()
@@ -1044,6 +1047,18 @@ IntLoadOpPattern::matchAndRewrite(memref::LoadOp loadOp,
10441047
shiftValue);
10451048
result = rewriter.create<spirv::ShiftRightArithmeticOp>(loc, dstType, result,
10461049
shiftValue);
1050+
1051+
if (isBool) {
1052+
dstType = typeConverter.convertType(loadOp.getType());
1053+
mask = spirv::ConstantOp::getOne(result.getType(), loc, rewriter);
1054+
Value isOne = rewriter.create<spirv::IEqualOp>(loc, result, mask);
1055+
Value zero = spirv::ConstantOp::getZero(dstType, loc, rewriter);
1056+
Value one = spirv::ConstantOp::getOne(dstType, loc, rewriter);
1057+
result = rewriter.create<spirv::SelectOp>(loc, dstType, isOne, one, zero);
1058+
} else if (result.getType().getIntOrFloatBitWidth() !=
1059+
static_cast<unsigned>(dstBits)) {
1060+
result = rewriter.create<spirv::SConvertOp>(loc, dstType, result);
1061+
}
10471062
rewriter.replaceOp(loadOp, result);
10481063

10491064
assert(accessChainOp.use_empty());
@@ -1117,6 +1132,10 @@ IntStoreOpPattern::matchAndRewrite(memref::StoreOp storeOp,
11171132
spirv::getElementPtr(typeConverter, memrefType, storeOperands.memref(),
11181133
storeOperands.indices(), loc, rewriter);
11191134
int srcBits = memrefType.getElementType().getIntOrFloatBitWidth();
1135+
1136+
bool isBool = srcBits == 1;
1137+
if (isBool)
1138+
srcBits = typeConverter.getOptions().boolNumBits;
11201139
auto dstType = typeConverter.convertType(memrefType)
11211140
.cast<spirv::PointerType>()
11221141
.getPointeeType()
@@ -1156,8 +1175,14 @@ IntStoreOpPattern::matchAndRewrite(memref::StoreOp storeOp,
11561175
rewriter.create<spirv::ShiftLeftLogicalOp>(loc, dstType, mask, offset);
11571176
clearBitsMask = rewriter.create<spirv::NotOp>(loc, dstType, clearBitsMask);
11581177

1159-
Value storeVal =
1160-
shiftValue(loc, storeOperands.value(), offset, mask, dstBits, rewriter);
1178+
Value storeVal = storeOperands.value();
1179+
if (isBool) {
1180+
Value zero = spirv::ConstantOp::getZero(dstType, loc, rewriter);
1181+
Value one = spirv::ConstantOp::getOne(dstType, loc, rewriter);
1182+
storeVal =
1183+
rewriter.create<spirv::SelectOp>(loc, dstType, storeVal, one, zero);
1184+
}
1185+
storeVal = shiftValue(loc, storeVal, offset, mask, dstBits, rewriter);
11611186
Value adjustedPtr = adjustAccessChainForBitwidth(typeConverter, accessChainOp,
11621187
srcBits, dstBits, rewriter);
11631188
Optional<spirv::Scope> scope = getAtomicOpScope(memrefType);

‎mlir/lib/Dialect/SPIRV/Transforms/SPIRVConversion.cpp‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ SPIRVTypeConverter::getStorageClassForMemorySpace(unsigned space) {
153153
#undef STORAGE_SPACE_MAP_FN
154154
}
155155

156+
const SPIRVTypeConverter::Options &SPIRVTypeConverter::getOptions() const {
157+
return options;
158+
}
159+
156160
#undef STORAGE_SPACE_MAP_LIST
157161

158162
// TODO: This is a utility function that should probably be exposed by the
@@ -342,9 +346,66 @@ static Type convertTensorType(const spirv::TargetEnv &targetEnv,
342346
return spirv::ArrayType::get(arrayElemType, arrayElemCount, *arrayElemSize);
343347
}
344348

349+
static Type convertBoolMemrefType(const spirv::TargetEnv &targetEnv,
350+
const SPIRVTypeConverter::Options &options,
351+
MemRefType type) {
352+
if (!type.hasStaticShape()) {
353+
LLVM_DEBUG(llvm::dbgs()
354+
<< type << " dynamic shape on i1 is not supported yet\n");
355+
return nullptr;
356+
}
357+
358+
Optional<spirv::StorageClass> storageClass =
359+
SPIRVTypeConverter::getStorageClassForMemorySpace(
360+
type.getMemorySpaceAsInt());
361+
if (!storageClass) {
362+
LLVM_DEBUG(llvm::dbgs()
363+
<< type << " illegal: cannot convert memory space\n");
364+
return nullptr;
365+
}
366+
367+
unsigned numBoolBits = options.boolNumBits;
368+
if (numBoolBits != 8) {
369+
LLVM_DEBUG(llvm::dbgs()
370+
<< "using non-8-bit storage for bool types unimplemented");
371+
return nullptr;
372+
}
373+
auto elementType = IntegerType::get(type.getContext(), numBoolBits)
374+
.dyn_cast<spirv::ScalarType>();
375+
if (!elementType)
376+
return nullptr;
377+
Type arrayElemType =
378+
convertScalarType(targetEnv, options, elementType, storageClass);
379+
if (!arrayElemType)
380+
return nullptr;
381+
Optional<int64_t> arrayElemSize = getTypeNumBytes(options, arrayElemType);
382+
if (!arrayElemSize) {
383+
LLVM_DEBUG(llvm::dbgs()
384+
<< type << " illegal: cannot deduce converted element size\n");
385+
return nullptr;
386+
}
387+
388+
int64_t memrefSize = (type.getNumElements() * numBoolBits + 7) / 8;
389+
auto arrayElemCount = (memrefSize + *arrayElemSize - 1) / *arrayElemSize;
390+
auto arrayType =
391+
spirv::ArrayType::get(arrayElemType, arrayElemCount, *arrayElemSize);
392+
393+
// Wrap in a struct to satisfy Vulkan interface requirements. Memrefs with
394+
// workgroup storage class do not need the struct to be laid out explicitly.
395+
auto structType = *storageClass == spirv::StorageClass::Workgroup
396+
? spirv::StructType::get(arrayType)
397+
: spirv::StructType::get(arrayType, 0);
398+
return spirv::PointerType::get(structType, *storageClass);
399+
}
400+
345401
static Type convertMemrefType(const spirv::TargetEnv &targetEnv,
346402
const SPIRVTypeConverter::Options &options,
347403
MemRefType type) {
404+
if (type.getElementType().isa<IntegerType>() &&
405+
type.getElementTypeBitWidth() == 1) {
406+
return convertBoolMemrefType(targetEnv, options, type);
407+
}
408+
348409
Optional<spirv::StorageClass> storageClass =
349410
SPIRVTypeConverter::getStorageClassForMemorySpace(
350411
type.getMemorySpaceAsInt());

‎mlir/test/Conversion/StandardToSPIRV/std-ops-to-spirv.mlir‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,12 +911,40 @@ func @load_store_zero_rank_int(%arg0: memref<i32>, %arg1: memref<i32>) {
911911

912912
// Check that access chain indices are properly adjusted if non-32-bit types are
913913
// emulated via 32-bit types.
914-
// TODO: Test i1 and i64 types.
914+
// TODO: Test i64 types.
915915
module attributes {
916916
spv.target_env = #spv.target_env<
917917
#spv.vce<v1.0, [Shader], [SPV_KHR_storage_buffer_storage_class]>, {}>
918918
} {
919919

920+
// CHECK-LABEL: @load_i1
921+
func @load_i1(%arg0: memref<i1>) -> i1 {
922+
// CHECK: %[[ZERO:.+]] = spv.Constant 0 : i32
923+
// CHECK: %[[FOUR1:.+]] = spv.Constant 4 : i32
924+
// CHECK: %[[QUOTIENT:.+]] = spv.SDiv %[[ZERO]], %[[FOUR1]] : i32
925+
// CHECK: %[[PTR:.+]] = spv.AccessChain %{{.+}}[%[[ZERO]], %[[QUOTIENT]]]
926+
// CHECK: %[[LOAD:.+]] = spv.Load "StorageBuffer" %[[PTR]]
927+
// CHECK: %[[FOUR2:.+]] = spv.Constant 4 : i32
928+
// CHECK: %[[EIGHT:.+]] = spv.Constant 8 : i32
929+
// CHECK: %[[IDX:.+]] = spv.UMod %[[ZERO]], %[[FOUR2]] : i32
930+
// CHECK: %[[BITS:.+]] = spv.IMul %[[IDX]], %[[EIGHT]] : i32
931+
// CHECK: %[[VALUE:.+]] = spv.ShiftRightArithmetic %[[LOAD]], %[[BITS]] : i32, i32
932+
// CHECK: %[[MASK:.+]] = spv.Constant 255 : i32
933+
// CHECK: %[[T1:.+]] = spv.BitwiseAnd %[[VALUE]], %[[MASK]] : i32
934+
// CHECK: %[[T2:.+]] = spv.Constant 24 : i32
935+
// CHECK: %[[T3:.+]] = spv.ShiftLeftLogical %[[T1]], %[[T2]] : i32, i32
936+
// CHECK: %[[T4:.+]] = spv.ShiftRightArithmetic %[[T3]], %[[T2]] : i32, i32
937+
// Convert to i1 type.
938+
// CHECK: %[[ONE:.+]] = spv.Constant 1 : i32
939+
// CHECK: %[[ISONE:.+]] = spv.IEqual %[[T4]], %[[ONE]] : i32
940+
// CHECK: %[[FALSE:.+]] = spv.Constant false
941+
// CHECK: %[[TRUE:.+]] = spv.Constant true
942+
// CHECK: %[[RES:.+]] = spv.Select %[[ISONE]], %[[TRUE]], %[[FALSE]] : i1, i1
943+
// CHECK: spv.ReturnValue %[[RES]] : i1
944+
%0 = memref.load %arg0[] : memref<i1>
945+
return %0 : i1
946+
}
947+
920948
// CHECK-LABEL: @load_i8
921949
func @load_i8(%arg0: memref<i8>) {
922950
// CHECK: %[[ZERO:.+]] = spv.Constant 0 : i32
@@ -982,6 +1010,31 @@ func @load_f32(%arg0: memref<f32>) {
9821010
return
9831011
}
9841012

1013+
// CHECK-LABEL: @store_i1
1014+
// CHECK: (%[[ARG0:.+]]: {{.*}}, %[[ARG1:.+]]: i1)
1015+
func @store_i1(%arg0: memref<i1>, %value: i1) {
1016+
// CHECK: %[[ZERO:.+]] = spv.Constant 0 : i32
1017+
// CHECK: %[[FOUR:.+]] = spv.Constant 4 : i32
1018+
// CHECK: %[[EIGHT:.+]] = spv.Constant 8 : i32
1019+
// CHECK: %[[IDX:.+]] = spv.UMod %[[ZERO]], %[[FOUR]] : i32
1020+
// CHECK: %[[OFFSET:.+]] = spv.IMul %[[IDX]], %[[EIGHT]] : i32
1021+
// CHECK: %[[MASK1:.+]] = spv.Constant 255 : i32
1022+
// CHECK: %[[TMP1:.+]] = spv.ShiftLeftLogical %[[MASK1]], %[[OFFSET]] : i32, i32
1023+
// CHECK: %[[MASK:.+]] = spv.Not %[[TMP1]] : i32
1024+
// CHECK: %[[ZERO1:.+]] = spv.Constant 0 : i32
1025+
// CHECK: %[[ONE1:.+]] = spv.Constant 1 : i32
1026+
// CHECK: %[[CASTED_ARG1:.+]] = spv.Select %[[ARG1]], %[[ONE1]], %[[ZERO1]] : i1, i32
1027+
// CHECK: %[[CLAMPED_VAL:.+]] = spv.BitwiseAnd %[[CASTED_ARG1]], %[[MASK1]] : i32
1028+
// CHECK: %[[STORE_VAL:.+]] = spv.ShiftLeftLogical %[[CLAMPED_VAL]], %[[OFFSET]] : i32, i32
1029+
// CHECK: %[[FOUR2:.+]] = spv.Constant 4 : i32
1030+
// CHECK: %[[ACCESS_IDX:.+]] = spv.SDiv %[[ZERO]], %[[FOUR2]] : i32
1031+
// CHECK: %[[PTR:.+]] = spv.AccessChain %[[ARG0]][%[[ZERO]], %[[ACCESS_IDX]]]
1032+
// CHECK: spv.AtomicAnd "Device" "AcquireRelease" %[[PTR]], %[[MASK]]
1033+
// CHECK: spv.AtomicOr "Device" "AcquireRelease" %[[PTR]], %[[STORE_VAL]]
1034+
memref.store %value, %arg0[] : memref<i1>
1035+
return
1036+
}
1037+
9851038
// CHECK-LABEL: @store_i8
9861039
// CHECK: (%[[ARG0:.+]]: {{.*}}, %[[ARG1:.+]]: i32)
9871040
func @store_i8(%arg0: memref<i8>, %value: i8) {

‎mlir/test/Conversion/StandardToSPIRV/std-types-to-spirv.mlir‎

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -286,27 +286,18 @@ func @memref_mem_space(
286286

287287
// -----
288288

289-
// Check that boolean memref is not supported at the moment.
290-
module attributes {
291-
spv.target_env = #spv.target_env<#spv.vce<v1.0, [], []>, {}>
292-
} {
293-
294-
// CHECK-LABEL: func @memref_type({{%.*}}: memref<3xi1>)
295-
func @memref_type(%arg0: memref<3xi1>) {
296-
return
297-
}
298-
299-
} // end module
300-
301-
// -----
302-
303289
// Check that using non-32-bit scalar types in interface storage classes
304290
// requires special capability and extension: convert them to 32-bit if not
305291
// satisfied.
306292
module attributes {
307293
spv.target_env = #spv.target_env<#spv.vce<v1.0, [], []>, {}>
308294
} {
309295

296+
// An i1 is store in 8-bit, so 5xi1 has 40 bits, which is stored in 2xi32.
297+
// CHECK-LABEL: spv.func @memref_1bit_type
298+
// CHECK-SAME: !spv.ptr<!spv.struct<(!spv.array<2 x i32, stride=4> [0])>, StorageBuffer>
299+
func @memref_1bit_type(%arg0: memref<5xi1>) { return }
300+
310301
// CHECK-LABEL: spv.func @memref_8bit_StorageBuffer
311302
// CHECK-SAME: !spv.ptr<!spv.struct<(!spv.array<16 x i32, stride=4> [0])>, StorageBuffer>
312303
// NOEMU-LABEL: func @memref_8bit_StorageBuffer

0 commit comments

Comments
 (0)