Skip to content

Commit 0e3e887

Browse files
adding spirv codegen
1 parent dbf19f6 commit 0e3e887

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@
3434
#include "clang/Frontend/FrontendDiagnostic.h"
3535
#include "llvm/ADT/APFloat.h"
3636
#include "llvm/ADT/APInt.h"
37+
#include "llvm/ADT/ArrayRef.h"
3738
#include "llvm/ADT/FloatingPointMode.h"
3839
#include "llvm/ADT/SmallPtrSet.h"
40+
#include "llvm/ADT/SmallVector.h"
3941
#include "llvm/ADT/StringExtras.h"
4042
#include "llvm/Analysis/ValueTracking.h"
4143
#include "llvm/IR/DataLayout.h"
@@ -19013,10 +19015,32 @@ case Builtin::BI__builtin_hlsl_elementwise_isinf: {
1901319015

1901419016
auto *Op0VecTy = E->getArg(0)->getType()->getAs<VectorType>();
1901519017

19016-
for(int idx = 0 ; idx < Op0VecTy -> getNumElements(); idx += 2){
19018+
int numElements = Op0VecTy -> getNumElements() * 2;
19019+
19020+
FixedVectorType *destTy = FixedVectorType::get(Int32Ty, numElements);
1901719021

19022+
Value *bitcast = Builder.CreateBitCast(Op0, destTy);
19023+
19024+
SmallVector<int> lowbitsIndex;
19025+
SmallVector<int> highbitsIndex;
19026+
19027+
for(int idx = 0; idx < numElements; idx += 2){
19028+
lowbitsIndex.push_back(idx);
19029+
}
19030+
19031+
for(int idx = 1; idx < numElements; idx += 2){
19032+
highbitsIndex.push_back(idx);
1901819033
}
1901919034

19035+
Value *arg0 = Builder.CreateShuffleVector(bitcast, lowbitsIndex);
19036+
Value *arg1 = Builder.CreateShuffleVector(bitcast, highbitsIndex);
19037+
19038+
Builder.CreateStore(arg0, Op1TmpLValue.getAddress());
19039+
auto *s = Builder.CreateStore(arg1, Op2TmpLValue.getAddress());
19040+
19041+
EmitWritebacks(*this, Args);
19042+
return s;
19043+
1902019044
}
1902119045
}
1902219046
return nullptr;

clang/test/CodeGenHLSL/builtins/splitdouble.hlsl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple dxil-pc-shadermodel6.3-library %s -fnative-half-type -emit-llvm -O1 -o - | FileCheck %s
2+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple spirv-vulkan-library %s -fnative-half-type -emit-llvm -O1 -o - | FileCheck %s --check-prefix=SPIRV
23

34

45

56
// CHECK: define {{.*}} i32 {{.*}}test_scalar{{.*}}(double {{.*}} [[VALD:%.*]])
67
// CHECK: [[VALRET:%.*]] = {{.*}} call { i32, i32 } @llvm.dx.splitdouble.i32(double [[VALD]])
78
// CHECK-NEXT: extractvalue { i32, i32 } [[VALRET]], 0
89
// CHECK-NEXT: extractvalue { i32, i32 } [[VALRET]], 1
10+
// SPIRV: define spir_func {{.*}} i32 {{.*}}test_scalar{{.*}}(double {{.*}} [[VALD:%.*]])
11+
// SPIRV-NOT: @llvm.dx.splitdouble.i32
12+
// SPIRV: [[CAST:%.*]] = bitcast double [[VALD]] to <2 x i32>
13+
// SPIRV-NEXT: extractelement <2 x i32> [[CAST]], i64 0
14+
// SPIRV-NEXT: extractelement <2 x i32> [[CAST]], i64 1
915
uint test_scalar(double D) {
1016
uint A, B;
1117
asuint(D, A, B);
@@ -17,6 +23,11 @@ uint test_scalar(double D) {
1723
// CHECK: [[VALRET:%.*]] = {{.*}} call { <3 x i32>, <3 x i32> } @llvm.dx.splitdouble.v3i32(<3 x double> [[VALD]])
1824
// CHECK-NEXT: extractvalue { <3 x i32>, <3 x i32> } [[VALRET]], 0
1925
// CHECK-NEXT: extractvalue { <3 x i32>, <3 x i32> } [[VALRET]], 1
26+
// SPIRV: define spir_func {{.*}} <3 x i32> {{.*}}test_vector{{.*}}(<3 x double> {{.*}} [[VALD:%.*]])
27+
// SPIRV-NOT: @llvm.dx.splitdouble.i32
28+
// SPIRV: [[CAST:%.*]] = bitcast <3 x double> [[VALD]] to <6 x i32>
29+
// SPIRV-NEXT: shufflevector <6 x i32> [[CAST]], <6 x i32> poison, <3 x i32> <i32 0, i32 2, i32 4>
30+
// SPIRV-NEXT: shufflevector <6 x i32> [[CAST]], <6 x i32> poison, <3 x i32> <i32 1, i32 3, i32 5>
2031
uint3 test_vector(double3 D) {
2132
uint3 A, B;
2233
asuint(D, A, B);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
; RUN: llc -verify-machineinstrs -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 lowering is correctly generating spirv code.
5+
6+
define spir_func noundef i32 @test_scalar(double noundef %D) local_unnamed_addr {
7+
entry:
8+
; CHECK: %[[#]] = OpBitcast %[[#]] %[[#]]
9+
%0 = bitcast double %D to <2 x i32>
10+
; CHECK: %[[#]] = OpCompositeExtract %[[#]] %[[#]] 0
11+
%1 = extractelement <2 x i32> %0, i64 0
12+
; CHECK: %[[#]] = OpCompositeExtract %[[#]] %[[#]] 1
13+
%2 = extractelement <2 x i32> %0, i64 1
14+
%add = add i32 %1, %2
15+
ret i32 %add
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
; RUN: opt -S -scalarizer -mtriple=spirv-vulkan-library %s 2>&1 | llc -verify-machineinstrs -O0 -mtriple=spirv-unknown-unknown -o - | FileCheck %s
2+
3+
; SPIRV lowering for splitdouble should relly on the scalarizer.
4+
5+
define spir_func noundef <3 x i32> @test_vector(<3 x double> noundef %D) local_unnamed_addr {
6+
entry:
7+
; CHECK-COUNT-3: %[[#]] = OpBitcast %[[#]] %[[#]]
8+
; CHECK-COUNT-3: %[[#]] = OpCompositeExtract %[[#]] %[[#]] [[0-2]]
9+
%0 = bitcast <3 x double> %D to <6 x i32>
10+
%1 = shufflevector <6 x i32> %0, <6 x i32> poison, <3 x i32> <i32 0, i32 2, i32 4>
11+
%2 = shufflevector <6 x i32> %0, <6 x i32> poison, <3 x i32> <i32 1, i32 3, i32 5>
12+
%add = add <3 x i32> %1, %2
13+
ret <3 x i32> %add
14+
}

0 commit comments

Comments
 (0)