Skip to content

Commit 9ef0481

Browse files
authored
Avoid OOB index when fixing constant array-initialization (microsoft#6825)
If the index on a constant store into an array is negative or out of bounds, that's an error, but shouldn't make the compiler index a vector out of bounds. Fixed: microsoft#6824
1 parent aa048b8 commit 9ef0481

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

lib/Transforms/Scalar/DxilFixConstArrayInitializer.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ static bool TryFixGlobalVariable(
119119

120120
GEPOperator *GEP = cast<GEPOperator>(Store->getPointerOperand());
121121
uint64_t Index = cast<ConstantInt>(GEP->getOperand(2))->getLimitedValue();
122+
if (Index >= LatestStores.size()) {
123+
// Skip out of bounds index.
124+
continue;
125+
}
122126

123127
if (LatestStores[Index] <= StoreIndex) {
124128
InitValue[Index] = cast<Constant>(Store->getValueOperand());
@@ -182,4 +186,4 @@ Pass *llvm::createDxilFixConstArrayInitializerPass() {
182186
}
183187

184188
INITIALIZE_PASS(DxilFixConstArrayInitializer, "dxil-fix-array-init",
185-
"Dxil Fix Array Initializer", false, false)
189+
"Dxil Fix Array Initializer", false, false)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
; RUN: %dxopt %s -hlsl-passes-resume -dxil-fix-array-init -S | FileCheck %s
2+
3+
; The pass should not perform an out of bounds access when trying to determine
4+
; which elements of an array are coverd by stores.
5+
; The store instructions may have out of bounds accesses, including to negative
6+
; indices. In these cases, ignore those stores. They are undefined behaviour
7+
; anyway, and the best thing to do with them in this pass is nothing.
8+
; If the original HLSL code used a literal -1 for the array index, the program
9+
; is rejected at an earlier stage of compilation.
10+
11+
; Issue: #6824
12+
13+
; Original HLSL:
14+
15+
; groupshared float4 w;
16+
;
17+
; [numthreads(1, 1, 1)]
18+
; void b() {
19+
; int i = -1;
20+
; w[i] = 0;
21+
; }
22+
23+
; Check that the store instruction remains, and the compiler does not crash.
24+
25+
; CHECK: define void @b
26+
; CHECK: store float 0
27+
; CHECK-SAME: , i32 0, i32 -1)
28+
; CHECK-NEXT: ret void
29+
30+
target datalayout = "e-m:e-p:32:32-i1:32-i8:32-i16:32-i32:32-i64:64-f16:32-f32:32-f64:64-n8:16:32:64"
31+
target triple = "dxil-ms-dx"
32+
33+
@"\01?w@@3V?$vector@M$03@@A.v" = addrspace(3) global [4 x float] undef, align 4
34+
35+
; Function Attrs: nounwind
36+
define void @b() #0 {
37+
entry:
38+
store float 0.000000e+00, float addrspace(3)* getelementptr inbounds ([4 x float], [4 x float] addrspace(3)* @"\01?w@@3V?$vector@M$03@@A.v", i32 0, i32 -1), !dbg !13, !tbaa !17 ; line:6 col:8
39+
ret void, !dbg !21 ; line:7 col:1
40+
}
41+
42+
attributes #0 = { nounwind }
43+
44+
!llvm.module.flags = !{!0}
45+
!pauseresume = !{!1}
46+
!llvm.ident = !{!2}
47+
!dx.version = !{!3}
48+
!dx.valver = !{!4}
49+
!dx.shaderModel = !{!5}
50+
!dx.typeAnnotations = !{!6}
51+
!dx.entryPoints = !{!10}
52+
53+
!0 = !{i32 2, !"Debug Info Version", i32 3}
54+
!1 = !{!"hlsl-dxilemit", !"hlsl-dxilload"}
55+
!2 = !{!"dxc(private) 1.8.0.4640 (issue-785, 45018c752d)"}
56+
!3 = !{i32 1, i32 0}
57+
!4 = !{i32 1, i32 8}
58+
!5 = !{!"cs", i32 6, i32 0}
59+
!6 = !{i32 1, void ()* @b, !7}
60+
!7 = !{!8}
61+
!8 = !{i32 1, !9, !9}
62+
!9 = !{}
63+
!10 = !{void ()* @b, !"b", null, null, !11}
64+
!11 = !{i32 4, !12}
65+
!12 = !{i32 1, i32 1, i32 1}
66+
!13 = !DILocation(line: 6, column: 8, scope: !14)
67+
!14 = !DISubprogram(name: "b", scope: !15, file: !15, line: 4, type: !16, isLocal: false, isDefinition: true, scopeLine: 4, flags: DIFlagPrototyped, isOptimized: false, function: void ()* @b)
68+
!15 = !DIFile(filename: "a.hlsl", directory: "")
69+
!16 = !DISubroutineType(types: !9)
70+
!17 = !{!18, !18, i64 0}
71+
!18 = !{!"float", !19, i64 0}
72+
!19 = !{!"omnipotent char", !20, i64 0}
73+
!20 = !{!"Simple C/C++ TBAA"}
74+
!21 = !DILocation(line: 7, column: 1, scope: !14)

0 commit comments

Comments
 (0)