Skip to content

Commit

Permalink
adds support for SPV_EXT_arithmetic_fence (#2954)
Browse files Browse the repository at this point in the history
bashbaug
Member
bashbaug commented 4 days ago
See: https://github.khronos.org/SPIRV-Registry/extensions/EXT/SPV_EXT_arithmetic_fence.html

Removes all internal enums for the unpublished SPV_INTEL_arithmetic_fence extension and uses the SPV_EXT_arithmetic_fence support in the headers instead.
Registers the SPV_EXT_arithmetic_fence extension.
Uses the SPV_EXT_arithmetic_fence extension if it is enabled, otherwise uses the SPV_INTEL_arithmetic_fence extension if it is enabled (for compatibility), otherwise ignores the LLVM arithmetic fence intrinsic (the ignoring part is not new).
Updates the arithmetic fence test:
Ensures that the right extension support is declared, depending on the enabled extensions.
Ensures that the ArithmeticFenceEXT capability is declared when either extension is enabled. Note, the spelling for the capability is unconditionally the EXT version.
Ensures that the OpArithmeticFenceEXT is present when either extension is enabled.
Ensures that no extension, capability, or instruction is generated when neither extension is enabled.
  • Loading branch information
bashbaug authored Jan 13, 2025
1 parent f2d913c commit a35a761
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 23 deletions.
1 change: 1 addition & 0 deletions include/LLVMSPIRVExtensions.inc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ EXT(SPV_INTEL_fpga_invocation_pipelining_attributes)
EXT(SPV_INTEL_token_type)
EXT(SPV_INTEL_debug_module)
EXT(SPV_INTEL_runtime_aligned)
EXT(SPV_EXT_arithmetic_fence)
EXT(SPV_INTEL_arithmetic_fence)
EXT(SPV_INTEL_bfloat16_conversion)
EXT(SPV_INTEL_joint_matrix)
Expand Down
2 changes: 1 addition & 1 deletion lib/SPIRV/SPIRVReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2910,7 +2910,7 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
return mapValue(
BV, transArbFloatInst(static_cast<SPIRVInstruction *>(BV), BB, true));

case internal::OpArithmeticFenceINTEL: {
case OpArithmeticFenceEXT: {
IRBuilder<> Builder(BB);
auto *BC = static_cast<SPIRVUnary *>(BV);
Type *RetTy = transType(BC->getType());
Expand Down
11 changes: 9 additions & 2 deletions lib/SPIRV/SPIRVWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4930,10 +4930,17 @@ SPIRVValue *LLVMToSPIRVBase::transIntrinsicInst(IntrinsicInst *II,
case Intrinsic::arithmetic_fence: {
SPIRVType *Ty = transType(II->getType());
SPIRVValue *Op = transValue(II->getArgOperand(0), BB);
if (BM->isAllowedToUseExtension(ExtensionID::SPV_EXT_arithmetic_fence)) {
BM->addCapability(CapabilityArithmeticFenceEXT);
BM->addExtension(ExtensionID::SPV_EXT_arithmetic_fence);
return BM->addUnaryInst(OpArithmeticFenceEXT, Ty, Op, BB);
}
if (BM->isAllowedToUseExtension(ExtensionID::SPV_INTEL_arithmetic_fence)) {
BM->addCapability(internal::CapabilityFPArithmeticFenceINTEL);
// Note: SPV_INTEL_arithmetic_fence was unpublished and superseded by
// SPV_EXT_arithmetic_fence.
BM->addCapability(CapabilityArithmeticFenceEXT);
BM->addExtension(ExtensionID::SPV_INTEL_arithmetic_fence);
return BM->addUnaryInst(internal::OpArithmeticFenceINTEL, Ty, Op, BB);
return BM->addUnaryInst(OpArithmeticFenceEXT, Ty, Op, BB);
}
return Op;
}
Expand Down
4 changes: 1 addition & 3 deletions lib/SPIRV/libSPIRV/SPIRVInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -1707,10 +1707,8 @@ _SPIRV_OP(SignBitSet)
_SPIRV_OP(Any)
_SPIRV_OP(All)
_SPIRV_OP(BitCount)
_SPIRV_OP(ArithmeticFenceEXT)
#undef _SPIRV_OP
#define _SPIRV_OP_INTERNAL(x) typedef SPIRVUnaryInst<internal::Op##x> SPIRV##x;
_SPIRV_OP_INTERNAL(ArithmeticFenceINTEL)
#undef _SPIRV_OP_INTERNAL

class SPIRVAccessChainBase : public SPIRVInstTemplateBase {
public:
Expand Down
2 changes: 1 addition & 1 deletion lib/SPIRV/libSPIRV/SPIRVNameMapEnum.h
Original file line number Diff line number Diff line change
Expand Up @@ -640,10 +640,10 @@ template <> inline void SPIRVMap<Capability, std::string>::init() {
add(CapabilityFPGALatencyControlINTEL, "FPGALatencyControlINTEL");
add(CapabilityFPMaxErrorINTEL, "FPMaxErrorINTEL");
add(CapabilityRegisterLimitsINTEL, "RegisterLimitsINTEL");
add(CapabilityArithmeticFenceEXT, "ArithmeticFenceEXT");
// From spirv_internal.hpp
add(internal::CapabilityFastCompositeINTEL, "FastCompositeINTEL");
add(internal::CapabilityTokenTypeINTEL, "TokenTypeINTEL");
add(internal::CapabilityFPArithmeticFenceINTEL, "FPArithmeticFenceINTEL");
add(internal::CapabilityBfloat16ConversionINTEL, "Bfloat16ConversionINTEL");
add(internal::CapabilityJointMatrixINTEL, "JointMatrixINTEL");
add(internal::CapabilityHWThreadQueryINTEL, "HWThreadQueryINTEL");
Expand Down
1 change: 1 addition & 0 deletions lib/SPIRV/libSPIRV/SPIRVOpCodeEnum.h
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ _SPIRV_OP(ConstantCompositeContinuedINTEL, 6091)
_SPIRV_OP(SpecConstantCompositeContinuedINTEL, 6092)
_SPIRV_OP(ControlBarrierArriveINTEL, 6142)
_SPIRV_OP(ControlBarrierWaitINTEL, 6143)
_SPIRV_OP(ArithmeticFenceEXT, 6145)
_SPIRV_OP(GroupIMulKHR, 6401)
_SPIRV_OP(GroupFMulKHR, 6402)
_SPIRV_OP(GroupBitwiseAndKHR, 6403)
Expand Down
1 change: 0 additions & 1 deletion lib/SPIRV/libSPIRV/SPIRVOpCodeEnumInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

_SPIRV_OP_INTERNAL(Forward, internal::OpForward)
_SPIRV_OP_INTERNAL(TypeTokenINTEL, internal::OpTypeTokenINTEL)
_SPIRV_OP_INTERNAL(ArithmeticFenceINTEL, internal::OpArithmeticFenceINTEL)
_SPIRV_OP_INTERNAL(ConvertFToBF16INTEL, internal::OpConvertFToBF16INTEL)
_SPIRV_OP_INTERNAL(ConvertBF16ToFINTEL, internal::OpConvertBF16ToFINTEL)
_SPIRV_OP_INTERNAL(TypeJointMatrixINTEL, internal::OpTypeJointMatrixINTEL)
Expand Down
5 changes: 0 additions & 5 deletions lib/SPIRV/libSPIRV/spirv_internal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ enum InternalOp {
IOpJointMatrixSUMadINTEL = 6128,
IOpJointMatrixUSMadINTEL = 6129,
IOpJointMatrixUUMadINTEL = 6130,
IOpArithmeticFenceINTEL = 6145,
IOpTaskSequenceCreateINTEL = 6163,
IOpTaskSequenceAsyncINTEL = 6164,
IOpTaskSequenceGetINTEL = 6165,
Expand Down Expand Up @@ -111,7 +110,6 @@ enum InternalCapability {
ICapBfloat16ConversionINTEL = 6115,
ICapabilityJointMatrixINTEL = 6118,
ICapabilityHWThreadQueryINTEL = 6134,
ICapFPArithmeticFenceINTEL = 6144,
ICapGlobalVariableDecorationsINTEL = 6146,
ICapabilityTaskSequenceINTEL = 6162,
ICapabilityCooperativeMatrixCheckedInstructionsINTEL = 6192,
Expand Down Expand Up @@ -269,7 +267,6 @@ constexpr SourceLanguage SourceLanguageCPP20 =

constexpr Op OpForward = static_cast<Op>(IOpForward);
constexpr Op OpTypeTokenINTEL = static_cast<Op>(IOpTypeTokenINTEL);
constexpr Op OpArithmeticFenceINTEL = static_cast<Op>(IOpArithmeticFenceINTEL);
constexpr Op OpConvertFToBF16INTEL = static_cast<Op>(IOpConvertFToBF16INTEL);
constexpr Op OpConvertBF16ToFINTEL = static_cast<Op>(IOpConvertBF16ToFINTEL);

Expand All @@ -290,8 +287,6 @@ constexpr Capability CapabilityFastCompositeINTEL =
static_cast<Capability>(ICapFastCompositeINTEL);
constexpr Capability CapabilityTokenTypeINTEL =
static_cast<Capability>(ICapTokenTypeINTEL);
constexpr Capability CapabilityFPArithmeticFenceINTEL =
static_cast<Capability>(ICapFPArithmeticFenceINTEL);
constexpr Capability CapabilityBfloat16ConversionINTEL =
static_cast<Capability>(ICapBfloat16ConversionINTEL);
constexpr Capability CapabilityGlobalVariableDecorationsINTEL =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
; RUN: llvm-as %s -o %t.bc
; RUN: llvm-spirv %t.bc --spirv-ext=+SPV_INTEL_arithmetic_fence -o %t.spv
; RUN: llvm-spirv %t.spv --to-text -o %t.spt
; RUN: FileCheck < %t.spt %s --check-prefix=CHECK-SPIRV
; RUN: llvm-spirv %t.bc --spirv-ext=+SPV_EXT_arithmetic_fence -spirv-text -o - | FileCheck %s --check-prefixes=CHECK-SPIRV-EXT,CHECK-SPIRV
; RUN: llvm-spirv %t.bc --spirv-ext=+SPV_INTEL_arithmetic_fence -spirv-text -o - | FileCheck %s --check-prefixes=CHECK-SPIRV-INTEL,CHECK-SPIRV
; RUN: llvm-spirv %t.bc --spirv-ext=+SPV_EXT_arithmetic_fence,+SPV_INTEL_arithmetic_fence -spirv-text -o - | FileCheck %s --check-prefixes=CHECK-SPIRV-EXT,CHECK-SPIRV


; RUN: llvm-spirv %t.bc --spirv-ext=+SPV_EXT_arithmetic_fence -o %t.spv
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
; RUN: llvm-dis < %t.rev.bc | FileCheck %s --check-prefix=CHECK-LLVM


; RUN: llvm-spirv %t.bc -o %t.negative.spv
; RUN: llvm-spirv %t.negative.spv --to-text -o %t.negative.spt
; RUN: FileCheck < %t.negative.spt %s --check-prefix=CHECK-SPIRV-NEG
; RUN: llvm-spirv %t.negative.spv --to-text -o - | FileCheck %s --check-prefix=CHECK-SPIRV-NEG


; RUN: llvm-spirv -r %t.negative.spv -o %t.negative.rev.bc
; RUN: llvm-dis < %t.negative.rev.bc | FileCheck %s --check-prefix=CHECK-LLVM-NEG

; CHECK-SPIRV: Capability FPArithmeticFenceINTEL
; CHECK-SPIRV: Extension "SPV_INTEL_arithmetic_fence"

; Note: The capability is unconditionally printed with the EXT suffix.
; CHECK-SPIRV: Capability ArithmeticFenceEXT

; CHECK-SPIRV-EXT: Extension "SPV_EXT_arithmetic_fence"
; CHECK-SPIRV-INTEL: Extension "SPV_INTEL_arithmetic_fence"

; CHECK-SPIRV: Name [[#Res:]] "t"
; CHECK-SPIRV: TypeFloat [[#ResTy:]] 64
; CHECK-SPIRV: FAdd [[#ResTy]] [[#Target:]]
; CHECK-SPIRV: ArithmeticFenceINTEL [[#ResTy]] [[#Res]] [[#Target]]
; Note: The instruction is unconditional printed with the EXT suffix.
; CHECK-SPIRV: ArithmeticFenceEXT [[#ResTy]] [[#Res]] [[#Target]]

; CHECK-LLVM: [[#Op:]] = fadd fast double %a, %a
; CHECK-LLVM: %t = call double @llvm.arithmetic.fence.f64(double %[[#Op]])
; CHECK-LLVM: declare double @llvm.arithmetic.fence.f64(double)

; CHECK-SPIRV-NEG-NOT: Capability FPArithmeticFenceINTEL
; CHECK-SPIRV-NEG-NOT: Capability ArithmeticFenceEXT
; CHECK-SPIRV-NEG-NOT: Extension "SPV_EXT_arithmetic_fence"
; CHECK-SPIRV-NEG-NOT: Extension "SPV_INTEL_arithmetic_fence"
; CHECK-SPIRV-NEG-NOT: ArithmeticFenceINTEL
; CHECK-SPIRV-NEG-NOT: ArithmeticFenceEXT

; CHECK-LLVM-NEG-NOT: declare double @llvm.arithmetic.fence.f64(double)

Expand Down

0 comments on commit a35a761

Please sign in to comment.