Skip to content

Commit a75a62e

Browse files
authored
Fix for a bunch of fixed point integer SPIR-V instructions (#1213)
If the return type of an instruction is wider than 64-bit, then this instruction will return via 'sret' argument added into the arguments list. Here we reverse this, removing 'sret' argument and restoring the original return type. TODO: currently 'sret' parameter attribute is being lost during reverse translation for the mentioned case, need to fix it.
1 parent 4eb63bf commit a75a62e

File tree

3 files changed

+135
-11
lines changed

3 files changed

+135
-11
lines changed

lib/SPIRV/SPIRVWriter.cpp

+39-11
Original file line numberDiff line numberDiff line change
@@ -4127,19 +4127,27 @@ LLVMToSPIRVBase::transBuiltinToInstWithoutDecoration(Op OC, CallInst *CI,
41274127
// Literal S Literal I Literal rI Literal Q Literal O
41284128

41294129
Type *ResTy = CI->getType();
4130+
4131+
auto OpItr = CI->value_op_begin();
4132+
auto OpEnd = OpItr + CI->getNumArgOperands();
4133+
4134+
// If the return type of an instruction is wider than 64-bit, then this
4135+
// instruction will return via 'sret' argument added into the arguments
4136+
// list. Here we reverse this, removing 'sret' argument and restoring
4137+
// the original return type.
4138+
if (CI->hasStructRetAttr()) {
4139+
assert(ResTy->isVoidTy() && "Return type is not void");
4140+
ResTy = cast<PointerType>(OpItr->getType())->getElementType();
4141+
OpItr++;
4142+
}
4143+
41304144
SPIRVValue *Input =
4131-
transValue(CI->getOperand(0) /* A - integer input of any width */, BB);
4132-
4133-
std::vector<Value *> Operands = {
4134-
CI->getOperand(1) /* S - bool value, indicator of signedness */,
4135-
CI->getOperand(2) /* I - location of the fixed-point of the input */,
4136-
CI->getOperand(3) /* rI - location of the fixed-point of the result*/,
4137-
CI->getOperand(4) /* Quantization mode */,
4138-
CI->getOperand(5) /* Overflow mode */};
4145+
transValue(*OpItr++ /* A - integer input of any width */, BB);
4146+
41394147
std::vector<SPIRVWord> Literals;
4140-
for (auto *O : Operands) {
4141-
Literals.push_back(cast<llvm::ConstantInt>(O)->getZExtValue());
4142-
}
4148+
std::transform(OpItr, OpEnd, std::back_inserter(Literals), [](auto *O) {
4149+
return cast<llvm::ConstantInt>(O)->getZExtValue();
4150+
});
41434151

41444152
return BM->addFixedPointIntelInst(OC, transType(ResTy), Input, Literals,
41454153
BB);
@@ -4207,6 +4215,16 @@ LLVMToSPIRVBase::transBuiltinToInstWithoutDecoration(Op OC, CallInst *CI,
42074215
auto OpItr = CI->value_op_begin();
42084216
auto OpEnd = OpItr + CI->getNumArgOperands();
42094217

4218+
// If the return type of an instruction is wider than 64-bit, then this
4219+
// instruction will return via 'sret' argument added into the arguments
4220+
// list. Here we reverse this, removing 'sret' argument and restoring
4221+
// the original return type.
4222+
if (CI->hasStructRetAttr()) {
4223+
assert(ResTy->isVoidTy() && "Return type is not void");
4224+
ResTy = cast<PointerType>(OpItr->getType())->getElementType();
4225+
OpItr++;
4226+
}
4227+
42104228
SPIRVValue *InA = transValue(*OpItr++ /* A - input */, BB);
42114229

42124230
std::vector<SPIRVWord> Literals;
@@ -4265,6 +4283,16 @@ LLVMToSPIRVBase::transBuiltinToInstWithoutDecoration(Op OC, CallInst *CI,
42654283
auto OpItr = CI->value_op_begin();
42664284
auto OpEnd = OpItr + CI->getNumArgOperands();
42674285

4286+
// If the return type of an instruction is wider than 64-bit, then this
4287+
// instruction will return via 'sret' argument added into the arguments
4288+
// list. Here we reverse this, removing 'sret' argument and restoring
4289+
// the original return type.
4290+
if (CI->hasStructRetAttr()) {
4291+
assert(ResTy->isVoidTy() && "Return type is not void");
4292+
ResTy = cast<PointerType>(OpItr->getType())->getElementType();
4293+
OpItr++;
4294+
}
4295+
42684296
SPIRVValue *InA = transValue(*OpItr++ /* A - input */, BB);
42694297

42704298
std::vector<SPIRVWord> Literals;

test/transcoding/capability-arbitrary-precision-fixed-point-numbers.ll

+33
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
; CHECK-SPIRV: 4 TypeInt [[Ty_64:[0-9]+]] 64 0
125125
; CHECK-SPIRV: 4 TypeInt [[Ty_44:[0-9]+]] 44 0
126126
; CHECK-SPIRV: 4 TypeInt [[Ty_34:[0-9]+]] 34 0
127+
; CHECK-SPIRV: 4 TypeInt [[Ty_66:[0-9]+]] 66 0
127128

128129
; CHECK-SPIRV: 6 Load [[Ty_13]] [[Sqrt_InId:[0-9]+]]
129130
; CHECK-SPIRV-NEXT: 9 FixedSqrtINTEL [[Ty_5]] [[#]] [[Sqrt_InId]] 0 2 2 0 0
@@ -162,6 +163,9 @@
162163
; CHECK-SPIRV: 6 Load [[Ty_44]] [[Exp_InId:[0-9]+]]
163164
; CHECK-SPIRV-NEXT: 9 FixedExpINTEL [[Ty_34]] [[#]] [[Exp_InId]] 0 20 20 0 0
164165

166+
; CHECK-SPIRV: 6 Load [[Ty_34]] [[SinCos_InId:[0-9]+]]
167+
; CHECK-SPIRV-NEXT: 9 FixedSinCosINTEL [[Ty_66]] [[#]] [[SinCos_InId]] 1 3 2 0 0
168+
165169
; CHECK-LLVM: call i5 @intel_arbitrary_fixed_sqrt.i5.i13(i13 %[[#]], i1 false, i32 2, i32 2, i32 0, i32 0)
166170
; CHECK-LLVM: call i13 @intel_arbitrary_fixed_sqrt.i13.i5(i5 %[[#]], i1 false, i32 2, i32 2, i32 0, i32 0)
167171
; CHECK-LLVM: call i13 @intel_arbitrary_fixed_sqrt.i13.i5(i5 %[[#]], i1 false, i32 2, i32 2, i32 0, i32 0)
@@ -175,6 +179,7 @@
175179
; CHECK-LLVM: call i10 @intel_arbitrary_fixed_sincospi.i10.i13(i13 %[[#]], i1 false, i32 2, i32 2, i32 0, i32 0)
176180
; CHECK-LLVM: call i44 @intel_arbitrary_fixed_log.i44.i64(i64 %[[#]], i1 true, i32 24, i32 22, i32 0, i32 0)
177181
; CHECK-LLVM: call i34 @intel_arbitrary_fixed_exp.i34.i44(i44 %[[#]], i1 false, i32 20, i32 20, i32 0, i32 0)
182+
; CHECK-LLVM: call i66 @intel_arbitrary_fixed_sincos.i66.i34(i34 %[[#]], i1 true, i32 3, i32 2, i32 0, i32 0)
178183

179184
; ModuleID = 'ap_fixed.cpp'
180185
source_filename = "ap_fixed.cpp"
@@ -205,6 +210,8 @@ $_Z3logILi64ELi44ELb1ELi24ELi22EEvv = comdat any
205210

206211
$_Z3expILi44ELi34ELb0ELi20ELi20EEvv = comdat any
207212

213+
$_Z7sin_cosILi31ELi20ELb1ELi10ELi12EEvv_ = comdat any
214+
208215
; Function Attrs: norecurse
209216
define dso_local spir_kernel void @_ZTSZ4mainE15kernel_function() #0 !kernel_arg_addr_space !4 !kernel_arg_access_qual !4 !kernel_arg_type !4 !kernel_arg_base_type !4 !kernel_arg_type_qual !4 {
210217
entry:
@@ -237,6 +244,7 @@ entry:
237244
call spir_func void @_Z10sin_cos_piILi13ELi5ELb0ELi2ELi2EEvv()
238245
call spir_func void @_Z3logILi64ELi44ELb1ELi24ELi22EEvv()
239246
call spir_func void @_Z3expILi44ELi34ELb0ELi20ELi20EEvv()
247+
call spir_func void @_Z7sin_cosILi31ELi20ELb1ELi10ELi12EEvv_()
240248
ret void
241249
}
242250

@@ -476,6 +484,28 @@ entry:
476484
ret void
477485
}
478486

487+
; Function Attrs: norecurse nounwind
488+
define linkonce_odr dso_local spir_func void @_Z7sin_cosILi31ELi20ELb1ELi10ELi12EEvv_() #3 comdat {
489+
entry:
490+
%0 = alloca i34, align 8
491+
%1 = addrspacecast i34* %0 to i34 addrspace(4)*
492+
%2 = alloca i66, align 8
493+
%3 = addrspacecast i66* %2 to i66 addrspace(4)*
494+
%4 = bitcast i34* %0 to i8*
495+
call void @llvm.lifetime.start.p0i8(i64 8, i8* %4)
496+
%5 = bitcast i66* %2 to i8*
497+
call void @llvm.lifetime.start.p0i8(i64 16, i8* %5)
498+
%6 = load i34, i34 addrspace(4)* %1, align 8
499+
call spir_func void @_Z24__spirv_FixedSinCosINTELILi34ELi66EEU7_ExtIntIXmlLi2ET0_EEiU7_ExtIntIXT_EEibiiii(i66 addrspace(4)* sret(i66) align 8 %3, i34 %6, i1 zeroext true, i32 3, i32 2, i32 0, i32 0) #5
500+
%7 = load i66, i66 addrspace(4)* %3, align 8
501+
store i66 %7, i66 addrspace(4)* %3, align 8
502+
%8 = bitcast i66* %2 to i8*
503+
call void @llvm.lifetime.end.p0i8(i64 16, i8* %8)
504+
%9 = bitcast i34* %0 to i8*
505+
call void @llvm.lifetime.end.p0i8(i64 8, i8* %9)
506+
ret void
507+
}
508+
479509
; Function Attrs: nounwind
480510
declare dso_local spir_func signext i5 @_Z22__spirv_FixedSqrtINTELILi13ELi5EEU7_ExtIntIXT0_EEiU7_ExtIntIXT_EEibiiii(i13 signext, i1 zeroext, i32, i32, i32, i32) #4
481511

@@ -512,6 +542,9 @@ declare dso_local spir_func i44 @_Z21__spirv_FixedLogINTELILi64ELi44EEU7_ExtIntI
512542
; Function Attrs: nounwind
513543
declare dso_local spir_func i34 @_Z21__spirv_FixedExpINTELILi44ELi34EEU7_ExtIntIXT0_EEiU7_ExtIntIXT_EEibiiii(i44, i1 zeroext, i32, i32, i32, i32) #4
514544

545+
; Function Attrs: nounwind
546+
declare dso_local spir_func void @_Z24__spirv_FixedSinCosINTELILi34ELi66EEU7_ExtIntIXmlLi2ET0_EEiU7_ExtIntIXT_EEibiiii(i66 addrspace(4)* sret(i66) align 8, i34, i1 zeroext, i32, i32, i32, i32) #4
547+
515548
attributes #0 = { norecurse "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "sycl-module-id"="ap_fixed.cpp" "uniform-work-group-size"="true" "unsafe-fp-math"="false" "use-soft-float"="false" }
516549
attributes #1 = { argmemonly nounwind willreturn }
517550
attributes #2 = { inlinehint norecurse "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }

test/transcoding/capability-arbitrary-precision-floating-point.ll

+63
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@
466466
; CHECK-SPIRV: 4 TypeInt [[Ty_54:[0-9]+]] 54 0
467467
; CHECK-SPIRV: 4 TypeInt [[Ty_56:[0-9]+]] 56 0
468468
; CHECK-SPIRV: 4 TypeInt [[Ty_12:[0-9]+]] 12 0
469+
; CHECK-SPIRV: 4 TypeInt [[Ty_66:[0-9]+]] 66 0
469470
; CHECK-SPIRV: 2 TypeBool [[Ty_Bool:[0-9]+]]
470471

471472
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-n8:16:32:64"
@@ -534,6 +535,8 @@ define internal spir_func void @"_ZZ4mainENK3$_0clEv"(%"class._ZTSZ4mainE3$_0.an
534535
call spir_func void @_Z12ap_float_powILi8ELi8ELi9ELi9ELi10ELi10EEvv()
535536
call spir_func void @_Z13ap_float_powrILi18ELi35ELi19ELi35ELi20ELi35EEvv()
536537
call spir_func void @_Z13ap_float_pownILi4ELi7ELi10ELi5ELi9EEvv()
538+
call spir_func void @_Z15ap_float_sincosILi8ELi18ELi10ELi20EEvv_()
539+
call spir_func void @_Z14ap_float_atan2ILi7ELi16ELi7ELi17ELi8ELi18EEvv_()
537540
ret void
538541
}
539542

@@ -1562,6 +1565,60 @@ define linkonce_odr dso_local spir_func void @_Z13ap_float_pownILi4ELi7ELi10ELi5
15621565
ret void
15631566
}
15641567

1568+
; Function Attrs: norecurse nounwind
1569+
define linkonce_odr dso_local spir_func void @_Z15ap_float_sincosILi8ELi18ELi10ELi20EEvv_() #3 {
1570+
%1 = alloca i34, align 8
1571+
%2 = addrspacecast i34* %1 to i34 addrspace(4)*
1572+
%3 = alloca i66, align 8
1573+
%4 = addrspacecast i66* %3 to i66 addrspace(4)*
1574+
%5 = bitcast i34* %1 to i8*
1575+
call void @llvm.lifetime.start.p0i8(i64 8, i8* %5)
1576+
%6 = bitcast i66* %3 to i8*
1577+
call void @llvm.lifetime.start.p0i8(i64 16, i8* %6)
1578+
%7 = load i34, i34 addrspace(4)* %2, align 8
1579+
call spir_func void @_Z33__spirv_ArbitraryFloatSinCosINTELILi34ELi66EEU7_ExtIntIXmlLi2ET0_EEiU7_ExtIntIXT_EEiiiiii(i66 addrspace(4)* sret(i66) align 8 %4, i34 %7, i32 18, i32 20, i32 0, i32 2, i32 1) #5
1580+
; CHECK-SPIRV: 6 Load [[Ty_34]] [[SinCos_AId:[0-9]+]]
1581+
; CHECK-SPIRV-NEXT: 9 ArbitraryFloatSinCosINTEL [[Ty_66]] [[#]] [[SinCos_AId]] 18 20 0 2 1
1582+
; CHECK-LLVM: call i66 @intel_arbitrary_float_sincos.i66.i34(i34 %[[#]], i32 18, i32 20, i32 0, i32 2, i32 1)
1583+
%8 = load i66, i66 addrspace(4)* %4, align 8
1584+
store i66 %8, i66 addrspace(4)* %4, align 8
1585+
%9 = bitcast i34* %1 to i8*
1586+
call void @llvm.lifetime.end.p0i8(i64 8, i8* %9)
1587+
%10 = bitcast i66* %3 to i8*
1588+
call void @llvm.lifetime.end.p0i8(i64 16, i8* %10)
1589+
ret void
1590+
}
1591+
1592+
; Function Attrs: norecurse nounwind
1593+
define linkonce_odr dso_local spir_func void @_Z14ap_float_atan2ILi7ELi16ELi7ELi17ELi8ELi18EEvv_() #3 {
1594+
%1 = alloca i24, align 4
1595+
%2 = alloca i25, align 4
1596+
%3 = alloca i66, align 8
1597+
%4 = addrspacecast i66* %3 to i66 addrspace(4)*
1598+
%5 = bitcast i24* %1 to i8*
1599+
call void @llvm.lifetime.start.p0i8(i64 4, i8* %5) #5
1600+
%6 = bitcast i25* %2 to i8*
1601+
call void @llvm.lifetime.start.p0i8(i64 4, i8* %6) #5
1602+
%7 = bitcast i66* %3 to i8*
1603+
call void @llvm.lifetime.start.p0i8(i64 16, i8* %7) #5
1604+
%8 = load i24, i24* %1, align 4, !tbaa !91
1605+
%9 = load i25, i25* %2, align 4, !tbaa !13
1606+
call spir_func void @_Z32__spirv_ArbitraryFloatATan2INTELILi24ELi25ELi66EEU7_ExtIntIXT1_EEiU7_ExtIntIXT_EEiiU7_ExtIntIXT0_EEiiiiii(i66 addrspace(4)* sret(i66) align 8 %4, i24 signext %8, i32 16, i25 signext %9, i32 17, i32 18, i32 0, i32 2, i32 1) #5
1607+
; CHECK-SPIRV: 6 Load [[Ty_24]] [[ATan2_AId:[0-9]+]]
1608+
; CHECK-SPIRV-NEXT: 6 Load [[Ty_25]] [[ATan2_BId:[0-9]+]]
1609+
; CHECK-SPIRV-NEXT: 11 ArbitraryFloatATan2INTEL [[Ty_66]] [[#]] [[ATan2_AId]] 16 [[ATan2_BId]] 17 18 0 2 1
1610+
; CHECK-LLVM: call i66 @intel_arbitrary_float_atan2.i66.i24.i25(i24 %[[#]], i32 16, i25 %[[#]], i32 17, i32 18, i32 0, i32 2, i32 1)
1611+
%10 = load i66, i66 addrspace(4)* %4, align 8
1612+
store i66 %10, i66 addrspace(4)* %4, align 8
1613+
%11 = bitcast i66* %3 to i8*
1614+
call void @llvm.lifetime.end.p0i8(i64 16, i8* %11) #5
1615+
%12 = bitcast i25* %2 to i8*
1616+
call void @llvm.lifetime.end.p0i8(i64 4, i8* %12) #5
1617+
%13 = bitcast i24* %1 to i8*
1618+
call void @llvm.lifetime.end.p0i8(i64 4, i8* %13) #5
1619+
ret void
1620+
}
1621+
15651622
; Function Attrs: nounwind
15661623
declare dso_local spir_func i40 @_Z31__spirv_ArbitraryFloatCastINTELILi40ELi40EEU7_ExtIntIXT0_EEiU7_ExtIntIXT_EEiiiiii(i40, i32, i32, i32, i32, i32) #4
15671624

@@ -1688,6 +1745,12 @@ declare dso_local spir_func i56 @_Z31__spirv_ArbitraryFloatPowRINTELILi54ELi55EL
16881745
; Function Attrs: nounwind
16891746
declare dso_local spir_func signext i15 @_Z31__spirv_ArbitraryFloatPowNINTELILi12ELi10ELi15EEU7_ExtIntIXT1_EEiU7_ExtIntIXT_EEiiU7_ExtIntIXT0_EEiiiii(i12 signext, i32, i10 signext, i32, i32, i32, i32) #4
16901747

1748+
; Function Attrs: nounwind
1749+
declare dso_local spir_func void @_Z33__spirv_ArbitraryFloatSinCosINTELILi34ELi66EEU7_ExtIntIXmlLi2ET0_EEiU7_ExtIntIXT_EEiiiiii(i66 addrspace(4)* sret(i66) align 8, i34, i32, i32, i32, i32, i32) #4
1750+
1751+
; Function Attrs: nounwind
1752+
declare dso_local spir_func void @_Z32__spirv_ArbitraryFloatATan2INTELILi24ELi25ELi66EEU7_ExtIntIXT1_EEiU7_ExtIntIXT_EEiiU7_ExtIntIXT0_EEiiiiii(i66 addrspace(4)* sret(i66) align 8, i24 signext, i32, i25 signext, i32, i32, i32, i32, i32) #4
1753+
16911754
attributes #0 = { norecurse "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "uniform-work-group-size"="true" "unsafe-fp-math"="false" "use-soft-float"="false" }
16921755
attributes #1 = { argmemonly nounwind willreturn }
16931756
attributes #2 = { inlinehint norecurse "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }

0 commit comments

Comments
 (0)