Skip to content

Commit bd40352

Browse files
committed
add spirv backend
1 parent e3ca0f0 commit bd40352

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ class SPIRVInstructionSelector : public InstructionSelector {
238238
bool selectLog10(Register ResVReg, const SPIRVType *ResType,
239239
MachineInstr &I) const;
240240

241+
bool selectNormalize(Register ResVReg, const SPIRVType *ResType,
242+
MachineInstr &I) const;
243+
241244
bool selectSpvThreadId(Register ResVReg, const SPIRVType *ResType,
242245
MachineInstr &I) const;
243246

@@ -1349,6 +1352,23 @@ bool SPIRVInstructionSelector::selectFrac(Register ResVReg,
13491352
.constrainAllUses(TII, TRI, RBI);
13501353
}
13511354

1355+
bool SPIRVInstructionSelector::selectNormalize(Register ResVReg,
1356+
const SPIRVType *ResType,
1357+
MachineInstr &I) const {
1358+
1359+
assert(I.getNumOperands() == 3);
1360+
assert(I.getOperand(2).isReg());
1361+
MachineBasicBlock &BB = *I.getParent();
1362+
1363+
return BuildMI(BB, I, I.getDebugLoc(), TII.get(SPIRV::OpExtInst))
1364+
.addDef(ResVReg)
1365+
.addUse(GR.getSPIRVTypeID(ResType))
1366+
.addImm(static_cast<uint32_t>(SPIRV::InstructionSet::GLSL_std_450))
1367+
.addImm(GL::Normalize)
1368+
.addUse(I.getOperand(2).getReg())
1369+
.constrainAllUses(TII, TRI, RBI);
1370+
}
1371+
13521372
bool SPIRVInstructionSelector::selectRsqrt(Register ResVReg,
13531373
const SPIRVType *ResType,
13541374
MachineInstr &I) const {
@@ -2080,6 +2100,8 @@ bool SPIRVInstructionSelector::selectIntrinsic(Register ResVReg,
20802100
return selectFmix(ResVReg, ResType, I);
20812101
case Intrinsic::spv_frac:
20822102
return selectFrac(ResVReg, ResType, I);
2103+
case Intrinsic::spv_normalize:
2104+
return selectNormalize(ResVReg, ResType, I);
20832105
case Intrinsic::spv_rsqrt:
20842106
return selectRsqrt(ResVReg, ResType, I);
20852107
case Intrinsic::spv_lifetime_start:
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
; RUN: llc -O0 -mtriple=spirv-unknown-unknown %s -o - | FileCheck %s
2+
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-unknown-unknown %s -o - -filetype=obj | spirv-val %}
3+
4+
; Make sure SPIRV operation function calls for normalize are lowered correctly.
5+
6+
; CHECK-DAG: %[[#op_ext_glsl:]] = OpExtInstImport "GLSL.std.450"
7+
; CHECK-DAG: %[[#float_32:]] = OpTypeFloat 32
8+
; CHECK-DAG: %[[#float_16:]] = OpTypeFloat 16
9+
; CHECK-DAG: %[[#vec4_float_16:]] = OpTypeVector %[[#float_16]] 4
10+
; CHECK-DAG: %[[#vec4_float_32:]] = OpTypeVector %[[#float_32]] 4
11+
12+
define noundef <4 x half> @normalize_half4(<4 x half> noundef %a) {
13+
entry:
14+
; CHECK: %[[#arg0:]] = OpFunctionParameter %[[#]]
15+
; CHECK: %[[#]] = OpExtInst %[[#vec4_float_16]] %[[#op_ext_glsl]] Normalize %[[#arg0]]
16+
%hlsl.normalize = call <4 x half> @llvm.spv.normalize.v4f16(<4 x half> %a)
17+
ret <4 x half> %hlsl.normalize
18+
}
19+
20+
define noundef <4 x float> @normalize_float4(<4 x float> noundef %a) {
21+
entry:
22+
; CHECK: %[[#arg0:]] = OpFunctionParameter %[[#]]
23+
; CHECK: %[[#]] = OpExtInst %[[#vec4_float_32]] %[[#op_ext_glsl]] Normalize %[[#arg0]]
24+
%hlsl.normalize = call <4 x float> @llvm.spv.normalize.v4f32(<4 x float> %a)
25+
ret <4 x float> %hlsl.normalize
26+
}
27+
28+
declare <4 x half> @llvm.spv.normalize.v4f16(<4 x half>)
29+
declare <4 x float> @llvm.spv.normalize.v4f32(<4 x float>)

0 commit comments

Comments
 (0)