Skip to content

Commit d8d8bb5

Browse files
fadeevalvmaksimo
authored andcommitted
Add ReadNone attr for Builtin functions (#768)
* Add ReadNone attr for Builtin functions Signed-off-by: Aleksander Fadeev <[email protected]>
1 parent 9c26b47 commit d8d8bb5

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

llvm-spirv/lib/SPIRV/SPIRVReader.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -4256,6 +4256,8 @@ Instruction *SPIRVToLLVM::transOCLBuiltinFromExtInst(SPIRVExtInst *BC,
42564256
F->setCallingConv(CallingConv::SPIR_FUNC);
42574257
if (isFuncNoUnwind())
42584258
F->addFnAttr(Attribute::NoUnwind);
4259+
if (isFuncReadNone(UnmangledName))
4260+
F->addFnAttr(Attribute::ReadNone);
42594261
}
42604262
auto Args = transValue(BC->getValues(BArgs), F, BB);
42614263
SPIRVDBG(dbgs() << "[transOCLBuiltinFromExtInst] Function: " << *F
@@ -4567,3 +4569,34 @@ bool llvm::getSpecConstInfo(std::istream &IS,
45674569
}
45684570
return !IS.fail();
45694571
}
4572+
4573+
// clang-format off
4574+
const StringSet<> SPIRVToLLVM::BuiltInConstFunc {
4575+
"convert", "get_work_dim", "get_global_size", "sub_group_ballot_bit_count",
4576+
"get_global_id", "get_local_size", "get_local_id", "get_num_groups",
4577+
"get_group_id", "get_global_offset", "acos", "acosh", "acospi",
4578+
"asin", "asinh", "asinpi", "atan", "atan2", "atanh", "atanpi",
4579+
"atan2pi", "cbrt", "ceil", "copysign", "cos", "cosh", "cospi",
4580+
"erfc", "erf", "exp", "exp2", "exp10", "expm1", "fabs", "fdim",
4581+
"floor", "fma", "fmax", "fmin", "fmod", "ilogb", "ldexp", "lgamma",
4582+
"log", "log2", "log10", "log1p", "logb", "mad", "maxmag", "minmag",
4583+
"nan", "nextafter", "pow", "pown", "powr", "remainder", "rint",
4584+
"rootn", "round", "rsqrt", "sin", "sinh", "sinpi", "sqrt", "tan",
4585+
"tanh", "tanpi", "tgamma", "trunc", "half_cos", "half_divide", "half_exp",
4586+
"half_exp2", "half_exp10", "half_log", "half_log2", "half_log10", "half_powr",
4587+
"half_recip", "half_rsqrt", "half_sin", "half_sqrt", "half_tan", "native_cos",
4588+
"native_divide", "native_exp", "native_exp2", "native_exp10", "native_log",
4589+
"native_log2", "native_log10", "native_powr", "native_recip", "native_rsqrt",
4590+
"native_sin", "native_sqrt", "native_tan", "abs", "abs_diff", "add_sat", "hadd",
4591+
"rhadd", "clamp", "clz", "mad_hi", "mad_sat", "max", "min", "mul_hi", "rotate",
4592+
"sub_sat", "upsample", "popcount", "mad24", "mul24", "degrees", "mix", "radians",
4593+
"step", "smoothstep", "sign", "cross", "dot", "distance", "length", "normalize",
4594+
"fast_distance", "fast_length", "fast_normalize", "isequal", "isnotequal",
4595+
"isgreater", "isgreaterequal", "isless", "islessequal", "islessgreater",
4596+
"isfinite", "isinf", "isnan", "isnormal", "isordered", "isunordered", "signbit",
4597+
"any", "all", "bitselect", "select", "shuffle", "shuffle2", "get_image_width",
4598+
"get_image_height", "get_image_depth", "get_image_channel_data_type",
4599+
"get_image_channel_order", "get_image_dim", "get_image_array_size",
4600+
"get_image_array_size", "sub_group_inverse_ballot", "sub_group_ballot_bit_extract",
4601+
};
4602+
// clang-format on

llvm-spirv/lib/SPIRV/SPIRVReader.h

+7
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "SPIRVModule.h"
4545

4646
#include "llvm/ADT/DenseMap.h"
47+
#include "llvm/ADT/StringSet.h"
4748
#include "llvm/IR/GlobalValue.h" // llvm::GlobalValue::LinkageTypes
4849

4950
namespace llvm {
@@ -76,6 +77,7 @@ class SPIRVToLLVM {
7677
public:
7778
SPIRVToLLVM(Module *LLVMModule, SPIRVModule *TheSPIRVModule);
7879

80+
static const StringSet<> BuiltInConstFunc;
7981
std::string getOCLBuiltinName(SPIRVInstruction *BI);
8082
std::string getOCLConvertBuiltinName(SPIRVInstruction *BI);
8183
std::string getOCLGenericCastToPtrName(SPIRVInstruction *BI);
@@ -220,6 +222,11 @@ class SPIRVToLLVM {
220222
// OpenCL function always has NoUnwind attribute.
221223
// Change this if it is no longer true.
222224
bool isFuncNoUnwind() const { return true; }
225+
226+
bool isFuncReadNone(const std::string &Name) const {
227+
return BuiltInConstFunc.count(Name);
228+
}
229+
223230
bool isSPIRVCmpInstTransToLLVMInst(SPIRVInstruction *BI) const;
224231
bool isDirectlyTranslatedToOCL(Op OpCode) const;
225232
bool transOCLBuiltinsFromVariables();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
; RUN: llvm-as %s -o %t.bc
2+
; RUN: llvm-spirv %t.bc -o %t.spv
3+
; RUN: llvm-spirv -r %t.spv -o %t.bc
4+
; RUN: llvm-dis < %t.bc | FileCheck %s --check-prefix=CHECK-LLVM
5+
6+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
7+
target triple = "spir-unknown-unknown"
8+
9+
; Function Attrs: convergent nofree norecurse nounwind uwtable
10+
define dso_local spir_kernel void @test_builtin_readnone(double* nocapture readonly %a, double* nocapture %b) local_unnamed_addr #0 !kernel_arg_addr_space !3 !kernel_arg_access_qual !4 !kernel_arg_type !5 !kernel_arg_base_type !5 !kernel_arg_type_qual !6 {
11+
entry:
12+
%0 = load double, double* %a, align 8, !tbaa !7
13+
%call = tail call double @_Z3expd(double %0) #2
14+
store double %call, double* %b, align 8, !tbaa !7
15+
%1 = load double, double* %a, align 8, !tbaa !7
16+
%call1 = tail call double @_Z3cosd(double %1) #2
17+
store double %call1, double* %b, align 8, !tbaa !7
18+
ret void
19+
}
20+
21+
; Function Attrs: convergent nounwind readnone
22+
; CHECK-LLVM: declare{{.*}}@_Z3expd{{.*}}#[[#Attrs:]]
23+
declare dso_local double @_Z3expd(double) local_unnamed_addr #1
24+
25+
; Function Attrs: convergent nounwind readnone
26+
; CHECK-LLVM: declare{{.*}}@_Z3cosd{{.*}}#[[#Attrs]]
27+
declare dso_local double @_Z3cosd(double) local_unnamed_addr #1
28+
29+
attributes #0 = { convergent nofree norecurse nounwind uwtable "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" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" "uniform-work-group-size"="false" "unsafe-fp-math"="false" "use-soft-float"="false" }
30+
; CHECK-LLVM: attributes #[[#Attrs]] {{.*}} readnone
31+
attributes #1 = { convergent nounwind readnone "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" "unsafe-fp-math"="false" "use-soft-float"="false" }
32+
attributes #2 = { convergent nounwind readnone }
33+
34+
!llvm.module.flags = !{!0}
35+
!opencl.ocl.version = !{!1}
36+
!llvm.ident = !{!2}
37+
38+
!0 = !{i32 1, !"wchar_size", i32 4}
39+
!1 = !{i32 2, i32 0}
40+
!2 = !{!"clang version 12.0.0 (https://github.com/intel/llvm 275e05b9dc13deb44eb7c765d23e65358d6bd077)"}
41+
!3 = !{i32 1, i32 1}
42+
!4 = !{!"none", !"none"}
43+
!5 = !{!"double*", !"double*"}
44+
!6 = !{!"", !""}
45+
!7 = !{!8, !8, i64 0}
46+
!8 = !{!"double", !9, i64 0}
47+
!9 = !{!"omnipotent char", !10, i64 0}
48+
!10 = !{!"Simple C/C++ TBAA"}

0 commit comments

Comments
 (0)