Skip to content

Commit 9ee3f23

Browse files
authored
More aggressive reassociations (#6626)
Although DXC applied the upstream change, Reassociate: add global reassociation algorithm (llvm/llvm-project@b8a330c) in this PR (#6598), it still might overlook some obvious common factors. One case has been observed is: ``` %Float4_0 = call %dx.types.CBufRet.f32 @dx.op.cbufferLoadLegacy.f32(i32 59, %dx.types.Handle %1, i32 1) %Float4_0.w = extractvalue %dx.types.CBufRet.f32 %Float4_0, 3 %Float2_0 = call %dx.types.CBufRet.f32 @dx.op.cbufferLoadLegacy.f32(i32 59, %dx.types.Handle %1, i32 0) %Float2_0.y = extractvalue %dx.types.CBufRet.f32 %Float2_0, 1 /* %Float4_1 is redundant with %Float4_0 since they invokes cbufferLoadLegacy with same parameters */ %Float4_1 = call %dx.types.CBufRet.f32 @dx.op.cbufferLoadLegacy.f32(i32 59, %dx.types.Handle %1, i32 1) /* %Float4_1.w is redundant with %Float4_0.w */ %Float4_1.w = extractvalue %dx.types.CBufRet.f32 %Float4_1, 3 /* %Float2_1 is redundant with %Float2_0 since they invokes cbufferLoadLegacy with same parameters */ %Float2_1 = call %dx.types.CBufRet.f32 @dx.op.cbufferLoadLegacy.f32(i32 59, %dx.types.Handle %1, i32 0) /* %Float2_1.y is redundant with %Float2_0.y */ %Float2_1.y = extractvalue %dx.types.CBufRet.f32 %Float2_1, 1 .... %11 = fmul fast float %Float4_0.w, %10 %12 = fmul fast float %11, %Float2_0.y .... %14 = fmul fast float %Float4_1.w, %13 %15 = fmul fast float %14, %Float2_1.y (%Float4_0.w * %Float2_0.y) equals to (%Float4_1.w * %Float2_1.y), they should be reassociated to a common factor ``` The upstream change can't identify this common factor because DXC doesn't know (%Float4_0.w, %Float4_1.w) and (%Float2_0.y, %Float2_1.y) are redundant when running Reassociate pass. Those redundancies will be eliminated in GVN pass. For DXC can identify more common factors, this PR will aggressively run Reassociate pass again after GVN pass and then run GVN pass again to remove the redundancies generared in this run of Reassociate pass. Changing the order of floating point operations causes the precision issue. In case some shaders get unexpected results due to this PR, use "-opt-disable aggressive-reassociation" to disable this PR and roll back. This is part 3 of the fix for #6593.
1 parent 1ee70fd commit 9ee3f23

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

lib/Transforms/IPO/PassManagerBuilder.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,16 @@ void PassManagerBuilder::populateModulePassManager(
504504
}
505505

506506
// HLSL Change Begins.
507+
{
508+
// Run reassociate pass again after GVN since GVN will expose more
509+
// opportunities for reassociation.
510+
if (HLSLEnableAggressiveReassociation) {
511+
MPM.add(createReassociatePass(true)); // Reassociate expressions
512+
if (EnableGVN)
513+
MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
514+
}
515+
}
516+
507517
// Use value numbering to figure out if regions are equivalent, and branch to only one.
508518
MPM.add(createDxilSimpleGVNEliminateRegionPass());
509519
// HLSL don't allow memcpy and memset.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// RUN: %dxc -T cs_6_3 -E cs_main %s -opt-enable aggressive-reassociation | FileCheck %s -check-prefixes=CHECK,COMMON_FACTOR
2+
// RUN: %dxc -T cs_6_3 -E cs_main %s -opt-disable aggressive-reassociation | FileCheck %s -check-prefixes=CHECK,NO_COMMON_FACTOR
3+
4+
// Make sure DXC recognize the common factor and generate optimized dxils.
5+
6+
// CHECK: [[TMP_CBUF:%.*]] = call %dx.types.Handle @dx.op.createHandle(i32 57, i8 2, i32 0, i32 10, i1 false)
7+
8+
// CHECK: [[ELEM_1:%.*]] = call %dx.types.CBufRet.f32 @dx.op.cbufferLoadLegacy.f32(i32 59, %dx.types.Handle [[TMP_CBUF]], i32 1)
9+
// CHECK: [[FACTOR_SRC1:%.*]] = extractvalue %dx.types.CBufRet.f32 [[ELEM_1]], 2
10+
11+
// CHECK: [[ELEM_0:%.*]] = call %dx.types.CBufRet.f32 @dx.op.cbufferLoadLegacy.f32(i32 59, %dx.types.Handle [[TMP_CBUF]], i32 0)
12+
// CHECK: [[FACTOR_SRC0:%.*]] = extractvalue %dx.types.CBufRet.f32 [[ELEM_0]], 0
13+
14+
// COMMON_FACTOR: [[FACTOR:%.*]] = fmul fast float [[FACTOR_SRC0]], [[FACTOR_SRC1]]
15+
// COMMON_FACTOR: fmul fast float [[FACTOR]],
16+
// COMMON_FACTOR: fmul fast float [[FACTOR]],
17+
// COMMON_FACTOR: fmul fast float [[FACTOR]],
18+
19+
// NO_COMMON_FACTOR: [[EXPRESSION_0:%.*]] = fmul fast float [[FACTOR_SRC1]],
20+
// NO_COMMON_FACTOR: fmul fast float [[EXPRESSION_0]], [[FACTOR_SRC0]]
21+
// NO_COMMON_FACTOR: [[EXPRESSION_1:%.*]] = fmul fast float [[FACTOR_SRC1]],
22+
// NO_COMMON_FACTOR: fmul fast float [[EXPRESSION_1]], [[FACTOR_SRC0]]
23+
// NO_COMMON_FACTOR: [[EXPRESSION_2:%.*]] = fmul fast float [[FACTOR_SRC1]],
24+
// NO_COMMON_FACTOR: fmul fast float [[EXPRESSION_2]], [[FACTOR_SRC0]]
25+
26+
27+
cbuffer TemporalAAData : register ( b10 )
28+
{
29+
float2 viewportRelativeSize ;
30+
float4 outputDimensions ;
31+
}
32+
33+
Texture2D < float4 > HistoryColor : register ( t2 ) ;
34+
SamplerState s_Linear : register ( s1 ) ;
35+
RWTexture1D < float3 > outColorBuffer : register ( u0 ) ;
36+
37+
float4 Test ( in Texture2D < float4 > tex , in SamplerState linearSampler , in float2 uv )
38+
{
39+
float2 samplePos = uv;
40+
float2 texPos1 = floor ( samplePos - 0.5f ) + 0.5f ;
41+
42+
float2 texPos0 = texPos1 - 1 ;
43+
float2 texPos3 = texPos1 + 2 ;
44+
float2 texPos12 = texPos1 + samplePos;
45+
46+
// DXC should recognize (outputDimensions . zw * viewportRelativeSize) is a common factor.
47+
texPos0 *= outputDimensions . zw * viewportRelativeSize ;
48+
texPos3 *= outputDimensions . zw * viewportRelativeSize ;
49+
texPos12 *= outputDimensions . zw * viewportRelativeSize ;
50+
51+
float4 result = 0.0f ;
52+
result += tex . SampleLevel ( linearSampler , float2 ( texPos0 . x , texPos0 . y ) , 0.0f );
53+
result += tex . SampleLevel ( linearSampler , float2 ( texPos12 . x , texPos0 . y ) , 0.0f );
54+
result += tex . SampleLevel ( linearSampler , float2 ( texPos3 . x , texPos0 . y ) , 0.0f );
55+
result += tex . SampleLevel ( linearSampler , float2 ( texPos0 . x , texPos12 . y ) , 0.0f );
56+
result += tex . SampleLevel ( linearSampler , float2 ( texPos12 . x , texPos12 . y ) , 0.0f );
57+
result += tex . SampleLevel ( linearSampler , float2 ( texPos3 . x , texPos12 . y ) , 0.0f );
58+
result += tex . SampleLevel ( linearSampler , float2 ( texPos0 . x , texPos3 . y ) , 0.0f );
59+
result += tex . SampleLevel ( linearSampler , float2 ( texPos12 . x , texPos3 . y ) , 0.0f );
60+
result += tex . SampleLevel ( linearSampler , float2 ( texPos3 . x , texPos3 . y ) , 0.0f );
61+
return result ;
62+
}
63+
64+
[ numthreads ( 8 , 8 , 1 ) ] void cs_main ( uint3 GroupID : SV_GroupID , uint GroupIndex : SV_GroupIndex , uint3 GTID : SV_GroupThreadID , uint3 DispatchThreadID : SV_DispatchThreadID )
65+
{
66+
uint2 pixelCoord = GTID . xy ;
67+
68+
outColorBuffer [ pixelCoord.x ] = Test ( HistoryColor , s_Linear , outputDimensions . zw ) . rgb;
69+
}

0 commit comments

Comments
 (0)