-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Open
Labels
Description
The SPIR-V backend puts a global variable under the Function storage space.
Original source:
// repr_outside_fn.c
const int values[16] = {-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113};
int select_value(unsigned int n) {
return values[n];
}
clang -S -emit-llvm -O3 repr_outside_fn.c -o repr_outside_fn.ll
generates:
; repr_outside_fn.ll
; ModuleID = 'repr_outside_fn.c'
source_filename = "repr_outside_fn.c"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
@values = dso_local local_unnamed_addr constant [16 x i32] [i32 -127, i32 -104, i32 -83, i32 -65, i32 -49, i32 -35, i32 -22, i32 -10, i32 1, i32 13, i32 25, i32 38, i32 53, i32 69, i32 89, i32 113], align 16
; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none) uwtable
define dso_local i32 @select_value(i32 noundef %n) local_unnamed_addr #0 {
entry:
%idxprom = zext i32 %n to i64
%arrayidx = getelementptr inbounds [16 x i32], ptr @values, i64 0, i64 %idxprom
%0 = load i32, ptr %arrayidx, align 4, !tbaa !5
ret i32 %0
}
attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}
!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.6 (https://github.com/llvm/llvm-project.git e21dc4bd5474d04b8e62d7331362edcc5648d7e5)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
llc -mtriple=spirv64 repr_outside_fn.ll -o repr_outside_fn.spt
generates:
; repr_outside_fn.spt
OpCapability Kernel
OpCapability Addresses
OpCapability Linkage
OpCapability Int64
%1 = OpExtInstImport "OpenCL.std"
OpMemoryModel Physical64 OpenCL
OpSource OpenCL_CPP 100000
OpName %28 "n"
OpName %29 "select_value"
OpName %26 "values"
OpName %30 "idxprom"
OpDecorate %29 LinkageAttributes "select_value" Export
OpDecorate %26 Constant
OpDecorate %26 Alignment 16
%2 = OpTypeInt 32 0
%3 = OpTypeFunction %2 %2
%4 = OpTypeInt 64 0
%5 = OpConstant %2 16
%6 = OpTypeArray %2 %5
%7 = OpConstant %2 4294967169
%8 = OpConstant %2 4294967192
%9 = OpConstant %2 4294967213
%10 = OpConstant %2 4294967231
%11 = OpConstant %2 4294967247
%12 = OpConstant %2 4294967261
%13 = OpConstant %2 4294967274
%14 = OpConstant %2 4294967286
%15 = OpConstant %2 1
%16 = OpConstant %2 13
%17 = OpConstant %2 25
%18 = OpConstant %2 38
%19 = OpConstant %2 53
%20 = OpConstant %2 69
%21 = OpConstant %2 89
%22 = OpConstant %2 113
%23 = OpConstantComposite %6 %7 %8 %9 %10 %11 %12 %13 %14 %15 %16 %17 %18 %19 %20 %21 %22
%24 = OpConstantNull %4
%25 = OpTypePointer Function %6 ;;; Function storage class outside of function
%26 = OpVariable %25 Function %23 ;;; Function storage class outside of function
%27 = OpTypePointer Function %2 ;;; Function storage class outside of function
%29 = OpFunction %2 Pure %3 ; -- Begin function select_value
%28 = OpFunctionParameter %2
%33 = OpLabel
%30 = OpUConvert %4 %28
%31 = OpInBoundsPtrAccessChain %27 %26 %24 %30
%32 = OpLoad %2 %31 Aligned 4
OpReturnValue %32
OpFunctionEnd
; -- End function
The instructions %25
, %26
and %27
have Function storage class despite not being in a function.
Used LLVM 19.1.6 and x86_64 (see repr_outside_fn.ll for details).