Skip to content

Commit 07a5e00

Browse files
AlexVlxsvenvhsumesh-s-mcwbashbaugasudarsa
authored
Re-sync with upstream, fix BitCast noise (#7)
* Update after llvm::Triple changes (#3046) Update after llvm-project commit `979c275097a6 ("[IR] Store Triple in Module (NFC) (#129868)", 2025-03-06)`. * Corrected subnormal checking logic issubnormal(V) ==> unsigned(abs(V) -1) (#3036) Corrected sunormal checking logic in is_fpclass intrinsic issubnormal(V) ==> unsigned(abs(V) - 1) < (all mantissa bits set) corrected the testfile to check the corrected logic * properly handle LOD values when nontemporal image operand is present (#3050) Instead of checking that the image operands are equal to the LOD mask, check that the LOD bit is in the image operands. This way an LOD value of zero may be ignored even when image operands contains bits other than the LOD bit, such as for the SPIR-V 1.6 nontemporal image operand. fixes #3049 * [NFC] Add some missing includes (#3045) llvm-spirv.cpp was relying on transitive includes for these. Also remove an unneeded `<set>` include. * Emit error for LLVM bfloat type (#3047) Signed-off-by: Arvind Sudarsanam <[email protected]> * Update DebugInfo test after LLVM change (#3060) Update the pattern after llvm-project commit da0f9e75d858 ("Reland: [MC] output inlined-at debug info (#106230) (#130306)", 2025-03-11). --------- Signed-off-by: Arvind Sudarsanam <[email protected]> Co-authored-by: Sven van Haastregt <[email protected]> Co-authored-by: sumesh-s-mcw <[email protected]> Co-authored-by: Ben Ashbaugh <[email protected]> Co-authored-by: Arvind Sudarsanam <[email protected]>
1 parent 4476d03 commit 07a5e00

File tree

8 files changed

+103
-20
lines changed

8 files changed

+103
-20
lines changed

lib/SPIRV/SPIRVReader.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -1102,8 +1102,6 @@ Value *SPIRVToLLVM::transConvertInst(SPIRVValue *BV, Function *F,
11021102
Dst->getPointerAddressSpace()) &&
11031103
M->getTargetTriple().getVendor() == Triple::VendorType::AMD)
11041104
CO = Instruction::AddrSpaceCast;
1105-
else
1106-
return Src;
11071105
} else {
11081106
// OpBitcast need to be handled as a special-case when the source is a
11091107
// pointer and the destination is not a pointer, and where the source is not
@@ -3524,7 +3522,7 @@ Function *SPIRVToLLVM::transFunction(SPIRVFunction *BF, unsigned AS) {
35243522

35253523
// TODO: this is temporarily disabled as it breaks some more complex code
35263524
// patterns that are otherwise correctly(-ish) handled
3527-
if (M->getTargetTriple().getVendor() == Triple::VendorType::AMD)
3525+
if (M->getTargetTriple().getVendor() != Triple::VendorType::AMD)
35283526
validatePhiPredecessors(F);
35293527
transLLVMLoopMetadata(F);
35303528

lib/SPIRV/SPIRVToOCL.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -755,8 +755,10 @@ SPIRVToOCLBase::mutateCallImageOperands(CallInst *CI, StringRef NewFuncName,
755755
ConstantFP *LodVal = dyn_cast<ConstantFP>(Mutator.getArg(ImOpArgIndex));
756756
// If the image operand is LOD and its value is zero, drop it too.
757757
if (LodVal && LodVal->isNullValue() &&
758-
ImOpValue == ImageOperandsMask::ImageOperandsLodMask)
758+
ImOpValue & ImageOperandsMask::ImageOperandsLodMask) {
759759
Mutator.removeArgs(ImOpArgIndex, Mutator.arg_size() - ImOpArgIndex);
760+
ImOpValue &= ~ImageOperandsMask::ImageOperandsLodMask;
761+
}
760762
}
761763
}
762764
return Mutator;

lib/SPIRV/SPIRVWriter.cpp

+10-5
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,9 @@ SPIRVType *LLVMToSPIRVBase::transType(Type *T) {
397397
}
398398
}
399399

400+
// Emit error if type is bfloat. LLVM native bfloat type is not supported.
401+
BM->getErrorLog().checkError(!T->isBFloatTy(),
402+
SPIRVEC_UnsupportedLLVMBFloatType);
400403
if (T->isFloatingPointTy())
401404
return mapType(T, BM->addFloatType(T->getPrimitiveSizeInBits()));
402405

@@ -5078,7 +5081,7 @@ SPIRVValue *LLVMToSPIRVBase::transIntrinsicInst(IntrinsicInst *II,
50785081
const APInt Inf = APFloat::getInf(Semantics).bitcastToAPInt();
50795082
const APInt AllOneMantissa =
50805083
APFloat::getLargest(Semantics).bitcastToAPInt() & ~Inf;
5081-
5084+
const APInt OneValue = APInt(BitSize, 1);
50825085
// Some checks can be inverted tests for simple cases, for example
50835086
// simultaneous check for inf, normal, subnormal and zero is a check for
50845087
// non nan.
@@ -5187,8 +5190,10 @@ SPIRVValue *LLVMToSPIRVBase::transIntrinsicInst(IntrinsicInst *II,
51875190
BM->addUnaryInst(OpBitcast, OpSPIRVTy, InputFloat, BB);
51885191
auto *MantissaConst = transValue(
51895192
Constant::getIntegerValue(IntOpLLVMTy, AllOneMantissa), BB);
5193+
auto *ConstOne =
5194+
transValue(Constant::getIntegerValue(IntOpLLVMTy, OneValue), BB);
51905195
auto *MinusOne =
5191-
BM->addBinaryInst(OpISub, OpSPIRVTy, BitCastToInt, MantissaConst, BB);
5196+
BM->addBinaryInst(OpISub, OpSPIRVTy, BitCastToInt, ConstOne, BB);
51925197
auto *TestIsSubnormal =
51935198
BM->addCmpInst(OpULessThan, ResTy, MinusOne, MantissaConst, BB);
51945199
if (FPClass & fcPosSubnormal && FPClass & fcNegSubnormal)
@@ -7048,7 +7053,7 @@ bool isValidLLVMModule(Module *M, SPIRVErrorLog &ErrorLog) {
70487053
Triple TT(M->getTargetTriple());
70497054
if (!ErrorLog.checkError(isSupportedTriple(TT), SPIRVEC_InvalidTargetTriple,
70507055
"Actual target triple is " +
7051-
M->getTargetTriple().str()))
7056+
M->getTargetTriple().str()))
70527057
return false;
70537058

70547059
return true;
@@ -7149,7 +7154,7 @@ bool runSpirvBackend(Module *M, std::string &Result, std::string &ErrMsg,
71497154
? Triple::spirv64
71507155
: Triple::spirv32,
71517156
TargetTriple.getSubArch());
7152-
M->setTargetTriple(TargetTriple.str());
7157+
M->setTargetTriple(TargetTriple);
71537158
// We need to reset Data Layout to conform with the TargetMachine
71547159
M->setDataLayout("");
71557160
}
@@ -7158,7 +7163,7 @@ bool runSpirvBackend(Module *M, std::string &Result, std::string &ErrMsg,
71587163
TargetTriple.setTriple(DefaultTriple);
71597164
TargetTriple.setArch(TargetTriple.getArch(),
71607165
spirvVersionToSubArch(TranslatorOpts.getMaxVersion()));
7161-
M->setTargetTriple(TargetTriple.str());
7166+
M->setTargetTriple(TargetTriple);
71627167
}
71637168

71647169
// Translate the Module into SPIR-V

lib/SPIRV/libSPIRV/SPIRVErrorEnum.h

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ _SPIRV_OP(UnspecifiedMemoryModel, "Unspecified Memory Model.")
2828
_SPIRV_OP(RepeatedMemoryModel, "Expects a single OpMemoryModel instruction.")
2929
_SPIRV_OP(UnsupportedVarArgFunction,
3030
"Variadic functions other than 'printf' are not supported in SPIR-V.")
31+
_SPIRV_OP(UnsupportedLLVMBFloatType,
32+
"LLVM bfloat type is not supported in SPIR-V.")
3133

3234
/* This is the last error code to have a maximum valid value to compare to */
3335
_SPIRV_OP(InternalMaxErrorCode, "Unknown error code")

test/bfloat.ll

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
; Check that translator emits error for LLVM bfloat type
2+
; RUN: llvm-as %s -o %t.bc
3+
; RUN: not amd-llvm-spirv --spirv-ext=+all %t.bc -o %t.spv 2>&1 | FileCheck %s --check-prefix=CHECK-ERROR
4+
5+
; CHECK-ERROR: UnsupportedLLVMBFloatType: LLVM bfloat type is not supported in SPIR-V
6+
7+
target datalayout = "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024"
8+
target triple = "spir-unknown-unknown"
9+
10+
; Function Attrs: nounwind
11+
define spir_kernel void @testBFloat(bfloat %a, bfloat %b) {
12+
entry:
13+
%r1 = fmul bfloat %a, %b
14+
ret void
15+
}
16+
17+
!llvm.module.flags = !{!0}
18+
!opencl.ocl.version = !{!1}
19+
!opencl.spir.version = !{!1}
20+
21+
!0 = !{i32 1, !"wchar_size", i32 4}
22+
!1 = !{i32 2, i32 0}

test/image_operand_nontemporal.spvasm

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
; Tests the Nontemporal image operand that was added for SPIR-V 1.6.
2+
3+
; REQUIRES: spirv-as
4+
; RUN: spirv-as --target-env spv1.6 -o %t.spv %s
5+
; RUN: spirv-val %t.spv
6+
; RUN: amd-llvm-spirv -r %t.spv -o %t.rev.bc
7+
; RUN: llvm-dis %t.rev.bc
8+
; RUN: FileCheck < %t.rev.ll %s --check-prefix=CHECK-LLVM
9+
10+
OpCapability Addresses
11+
OpCapability Kernel
12+
OpCapability ImageBasic
13+
OpCapability LiteralSampler
14+
OpMemoryModel Physical64 OpenCL
15+
OpEntryPoint Kernel %kernel "read_write_image_nontemporal"
16+
%uint = OpTypeInt 32 0
17+
%void = OpTypeVoid
18+
%read_image2d_t = OpTypeImage %void 2D 0 0 0 0 Unknown ReadOnly
19+
%write_image2d_t = OpTypeImage %void 2D 0 0 0 0 Unknown WriteOnly
20+
%sampler_t = OpTypeSampler
21+
%kernel_sig = OpTypeFunction %void %read_image2d_t %write_image2d_t
22+
%sampledimage_t = OpTypeSampledImage %read_image2d_t
23+
%v2uint = OpTypeVector %uint 2
24+
%float = OpTypeFloat 32
25+
%v4float = OpTypeVector %float 4
26+
%sampler = OpConstantSampler %sampler_t None 0 Nearest
27+
%coord_0_0 = OpConstantNull %v2uint
28+
%float_0 = OpConstant %float 0
29+
%kernel = OpFunction %void None %kernel_sig
30+
%src = OpFunctionParameter %read_image2d_t
31+
%dst = OpFunctionParameter %write_image2d_t
32+
%entry = OpLabel
33+
%si = OpSampledImage %sampledimage_t %src %sampler
34+
%data0 = OpImageSampleExplicitLod %v4float %si %coord_0_0 Lod %float_0
35+
OpImageWrite %dst %coord_0_0 %data0
36+
%data1 = OpImageSampleExplicitLod %v4float %si %coord_0_0 Lod|Nontemporal %float_0
37+
OpImageWrite %dst %coord_0_0 %data1 Nontemporal
38+
OpReturn
39+
OpFunctionEnd
40+
41+
; CHECK-LLVM: define spir_kernel void @read_write_image_nontemporal
42+
; CHECK-LLVM: call spir_func <4 x float> [[READ_IMAGEF:@[a-zA-Z0-9_]+]](
43+
; CHECK-LLVM: call spir_func void [[WRITE_IMAGEF:@[a-zA-Z0-9_]+]](
44+
; CHECK-LLVM: call spir_func <4 x float> [[READ_IMAGEF]](
45+
; CHECK-LLVM: call spir_func void [[WRITE_IMAGEF]](

test/llvm-intrinsics/fpclass.ll

+14-10
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@
2929
; CHECK-SPIRV-DAG: Constant [[#Int16Ty]] [[#QNanBitConst16:]] 32256
3030
; CHECK-SPIRV-DAG: Constant [[#Int16Ty]] [[#MantissaConst16:]] 1023
3131
; CHECK-SPIRV-DAG: Constant [[#DoubleTy]] [[#DoubleConst:]] 0 1072693248
32-
; CHECK-SPIRV-DAG: ConstantComposite [[#Int16VecTy:]] [[#QNanBitConstVec16:]] [[#QNanBitConst16]] [[#QNanBitConst16]]
33-
; CHECK-SPIRV-DAG: ConstantComposite [[#Int16VecTy:]] [[#MantissaConstVec16:]] [[#MantissaConst16]] [[#MantissaConst16]]
32+
; CHECK-SPIRV-DAG: Constant [[#Int32Ty]] [[#const32One:]] 1
33+
; CHECK-SPIRV-DAG: Constant [[#Int64Ty]] [[#const64One:]] 1 0
34+
; CHECK-SPIRV-DAG: Constant [[#Int16Ty]] [[#const16One:]] 1
35+
; CHECK-SPIRV-DAG: ConstantComposite [[#Int16VecTy]] [[#const16vecOne:]] [[#const16One]] [[#const16One]]
36+
; CHECK-SPIRV-DAG: ConstantComposite [[#Int16VecTy]] [[#QNanBitConstVec16:]] [[#QNanBitConst16]] [[#QNanBitConst16]]
37+
; CHECK-SPIRV-DAG: ConstantComposite [[#Int16VecTy]] [[#MantissaConstVec16:]] [[#MantissaConst16]] [[#MantissaConst16]]
3438
; CHECK-SPIRV-DAG: ConstantNull [[#Int16VecTy]] [[#ZeroConst16:]]
3539
; CHECK-SPIRV-DAG: ConstantTrue [[#BoolTy]] [[#True:]]
3640
; CHECK-SPIRV-DAG: ConstantFalse [[#BoolTy]] [[#False:]]
@@ -262,8 +266,8 @@ define i1 @test_class_subnormal(float %arg) {
262266
; CHECK-SPIRV-EMPTY:
263267
; CHECK-SPIRV-NEXT: Label
264268
; CHECK-SPIRV-NEXT: Bitcast [[#Int32Ty]] [[#BitCast:]] [[#Val]]
265-
; CHECK-SPIRV-NEXT: ISub [[#Int32Ty]] [[#Sub:]] [[#BitCast]] [[#MantissaConst:]]
266-
; CHECK-SPIRV-NEXT: ULessThan [[#BoolTy]] [[#Less:]] [[#Sub]] [[#MantissaConst:]]
269+
; CHECK-SPIRV-NEXT: ISub [[#Int32Ty]] [[#Sub:]] [[#BitCast]] [[#const32One]]
270+
; CHECK-SPIRV-NEXT: ULessThan [[#BoolTy]] [[#Less:]] [[#Sub]] [[#MantissaConst]]
267271
; CHECK-SPIRV-NEXT: ReturnValue [[#Less]]
268272
%val = call i1 @llvm.is.fpclass.f32(float %arg, i32 144)
269273
ret i1 %val
@@ -276,8 +280,8 @@ define i1 @test_class_possubnormal(float %arg) {
276280
; CHECK-SPIRV-EMPTY:
277281
; CHECK-SPIRV-NEXT: Label
278282
; CHECK-SPIRV-NEXT: Bitcast [[#Int32Ty]] [[#BitCast:]] [[#Val]]
279-
; CHECK-SPIRV-NEXT: ISub [[#Int32Ty]] [[#Sub:]] [[#BitCast]] [[#MantissaConst:]]
280-
; CHECK-SPIRV-NEXT: ULessThan [[#BoolTy]] [[#Less:]] [[#Sub]] [[#MantissaConst:]]
283+
; CHECK-SPIRV-NEXT: ISub [[#Int32Ty]] [[#Sub:]] [[#BitCast]] [[#const32One]]
284+
; CHECK-SPIRV-NEXT: ULessThan [[#BoolTy]] [[#Less:]] [[#Sub]] [[#MantissaConst]]
281285
; CHECK-SPIRV-NEXT: SignBitSet [[#BoolTy]] [[#Sign:]] [[#Val]]
282286
; CHECK-SPIRV-NEXT: LogicalNot [[#BoolTy]] [[#Not:]] [[#Sign]]
283287
; CHECK-SPIRV-NEXT: LogicalAnd [[#BoolTy]] [[#And:]] [[#Not]] [[#Less]]
@@ -293,8 +297,8 @@ define i1 @test_class_negsubnormal(float %arg) {
293297
; CHECK-SPIRV-EMPTY:
294298
; CHECK-SPIRV-NEXT: Label
295299
; CHECK-SPIRV-NEXT: Bitcast [[#Int32Ty]] [[#BitCast:]] [[#Val]]
296-
; CHECK-SPIRV-NEXT: ISub [[#Int32Ty]] [[#Sub:]] [[#BitCast]] [[#MantissaConst:]]
297-
; CHECK-SPIRV-NEXT: ULessThan [[#BoolTy]] [[#Less:]] [[#Sub]] [[#MantissaConst:]]
300+
; CHECK-SPIRV-NEXT: ISub [[#Int32Ty]] [[#Sub:]] [[#BitCast]] [[#const32One]]
301+
; CHECK-SPIRV-NEXT: ULessThan [[#BoolTy]] [[#Less:]] [[#Sub]] [[#MantissaConst]]
298302
; CHECK-SPIRV-NEXT: SignBitSet [[#BoolTy]] [[#Sign:]] [[#Val]]
299303
; CHECK-SPIRV-NEXT: LogicalAnd [[#BoolTy]] [[#And:]] [[#Sign]] [[#Less]]
300304
; CHECK-SPIRV-NEXT: ReturnValue [[#And]]
@@ -376,7 +380,7 @@ define i1 @test_class_neginf_posnormal_negsubnormal_poszero_snan_f64(double %arg
376380
; CHECK-SPIRV-NEXT: LogicalNot [[#BoolTy]] [[#Not2:]] [[#Sign]]
377381
; CHECK-SPIRV-NEXT: LogicalAnd [[#BoolTy]] [[#And3:]] [[#Not2]] [[#IsNormal]]
378382
; CHECK-SPIRV-NEXT: Bitcast [[#Int64Ty]] [[#BitCast2:]] [[#Val]]
379-
; CHECK-SPIRV-NEXT: ISub [[#Int64Ty]] [[#Sub:]] [[#BitCast2]] [[#MantissaConst64]]
383+
; CHECK-SPIRV-NEXT: ISub [[#Int64Ty]] [[#Sub:]] [[#BitCast2]] [[#const64One]]
380384
; CHECK-SPIRV-NEXT: ULessThan [[#BoolTy]] [[#Less:]] [[#Sub]] [[#MantissaConst64]]
381385
; CHECK-SPIRV-NEXT: LogicalAnd [[#BoolTy]] [[#And4:]] [[#Sign]] [[#Less]]
382386
; CHECK-SPIRV-NEXT: Bitcast [[#Int64Ty]] [[#BitCast3:]] [[#Val]]
@@ -408,7 +412,7 @@ define <2 x i1> @test_class_neginf_posnormal_negsubnormal_poszero_snan_v2f16(<2
408412
; CHECK-SPIRV-NEXT: LogicalNot [[#VecBoolTy]] [[#Not2:]] [[#Sign]]
409413
; CHECK-SPIRV-NEXT: LogicalAnd [[#VecBoolTy]] [[#And3:]] [[#Not2]] [[#IsNormal]]
410414
; CHECK-SPIRV-NEXT: Bitcast [[#Int16VecTy]] [[#BitCast2:]] [[#Val]]
411-
; CHECK-SPIRV-NEXT: ISub [[#Int16VecTy]] [[#Sub:]] [[#BitCast2]] [[#MantissaConstVec16]]
415+
; CHECK-SPIRV-NEXT: ISub [[#Int16VecTy]] [[#Sub:]] [[#BitCast2]] [[#const16vecOne]]
412416
; CHECK-SPIRV-NEXT: ULessThan [[#VecBoolTy]] [[#Less:]] [[#Sub]] [[#MantissaConstVec16]]
413417
; CHECK-SPIRV-NEXT: LogicalAnd [[#VecBoolTy]] [[#And4:]] [[#Sign]] [[#Less]]
414418
; CHECK-SPIRV-NEXT: Bitcast [[#Int16VecTy]] [[#BitCast3:]] [[#Val]]

tools/llvm-spirv/llvm-spirv.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545
///
4646
//===----------------------------------------------------------------------===//
4747

48+
#include "llvm/ADT/APFloat.h"
49+
#include "llvm/ADT/APInt.h"
50+
#include "llvm/ADT/SmallVector.h"
51+
#include "llvm/ADT/StringRef.h"
4852
#include "llvm/Bitcode/BitcodeReader.h"
4953
#include "llvm/Bitcode/BitcodeWriter.h"
5054
#include "llvm/IR/Constants.h"
@@ -55,10 +59,12 @@
5559
#include "llvm/Support/Debug.h"
5660
#include "llvm/Support/Error.h"
5761
#include "llvm/Support/FileSystem.h"
62+
#include "llvm/Support/MathExtras.h"
5863
#include "llvm/Support/MemoryBuffer.h"
5964
#include "llvm/Support/PrettyStackTrace.h"
6065
#include "llvm/Support/Signals.h"
6166
#include "llvm/Support/ToolOutputFile.h"
67+
#include "llvm/Support/raw_ostream.h"
6268

6369
#ifdef LLVM_SPIRV_HAVE_SPIRV_TOOLS
6470
#include "spirv-tools/libspirv.hpp"
@@ -74,7 +80,6 @@
7480
#include <iostream>
7581
#include <map>
7682
#include <memory>
77-
#include <set>
7883
#include <sstream>
7984
#include <string>
8085

0 commit comments

Comments
 (0)