Skip to content

Commit 985b29b

Browse files
author
Greg Roth
authored
Allow removal of trivially dead convergent marker (#3643)
merge #3640 into release-1.6.2104 Failing to remove this because it is marked as having side effects so it can prevent unwanted code movement resulted in trivially dead code being retained unnecessarily because the marker isn't removed until after dead code elimination. By allowing its removal when the operation that needed it has been removed so it has no users, this dead code can be eliminated. (cherry picked from commit cf135fa)
1 parent b222608 commit 985b29b

7 files changed

Lines changed: 51 additions & 33 deletions

File tree

include/dxc/DXIL/DxilUtil.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ namespace dxilutil {
152152

153153
void ReplaceRawBufferLoad64Bit(llvm::Function *F, llvm::Type *EltTy, hlsl::OP *hlslOP);
154154
void ReplaceRawBufferStore64Bit(llvm::Function *F, llvm::Type *ETy, hlsl::OP *hlslOP);
155+
156+
bool IsConvergentMarker(llvm::Value *V);
157+
llvm::Value *GetConvergentSource(llvm::Value *V);
155158
}
156159

157160
}

include/dxc/HLSL/DxilConvergent.h

Lines changed: 0 additions & 19 deletions
This file was deleted.

lib/DXIL/DxilUtil.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "dxc/DXIL/DxilUtil.h"
1515
#include "dxc/DXIL/DxilModule.h"
1616
#include "dxc/DXIL/DxilOperations.h"
17+
#include "dxc/HLSL/DxilConvergentName.h"
1718
#include "dxc/Support/Global.h"
1819
#include "llvm/ADT/StringExtras.h"
1920
#include "llvm/ADT/Twine.h"
@@ -1171,6 +1172,26 @@ void ReplaceRawBufferStore64Bit(llvm::Function *F, llvm::Type *ETy, hlsl::OP *hl
11711172
}
11721173
}
11731174

1175+
bool IsConvergentMarker(const char *Name) {
1176+
StringRef RName = Name;
1177+
return RName.startswith(kConvergentFunctionPrefix);
1178+
}
1179+
1180+
bool IsConvergentMarker(const Function *F) {
1181+
return F && F->getName().startswith(kConvergentFunctionPrefix);
1182+
}
1183+
1184+
bool IsConvergentMarker(Value *V) {
1185+
CallInst *CI = dyn_cast<CallInst>(V);
1186+
if (!CI)
1187+
return false;
1188+
return IsConvergentMarker(CI->getCalledFunction());
1189+
}
1190+
1191+
Value *GetConvergentSource(Value *V) {
1192+
return cast<CallInst>(V)->getOperand(0);
1193+
}
1194+
11741195
}
11751196
}
11761197

lib/HLSL/DxilConvergent.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,13 @@
2222
#include "dxc/HLSL/DxilGenerationPass.h"
2323
#include "dxc/HLSL/HLOperations.h"
2424
#include "dxc/HLSL/HLModule.h"
25-
#include "dxc/HLSL/DxilConvergent.h"
2625
#include "dxc/HlslIntrinsicOp.h"
2726
#include "dxc/HLSL/DxilConvergentName.h"
2827

2928
using namespace llvm;
3029
using namespace hlsl;
3130

32-
bool hlsl::IsConvergentMarker(Value *V) {
33-
CallInst *CI = dyn_cast<CallInst>(V);
34-
if (!CI)
35-
return false;
36-
Function *F = CI->getCalledFunction();
37-
return F->getName().startswith(kConvergentFunctionPrefix);
38-
}
3931

40-
Value *hlsl::GetConvergentSource(Value *V) {
41-
return cast<CallInst>(V)->getOperand(0);
42-
}
4332

4433
///////////////////////////////////////////////////////////////////////////////
4534
// DxilConvergent.

lib/HLSL/HLOperationLower.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "dxc/HLSL/HLOperationLowerExtension.h"
2626
#include "dxc/HLSL/HLOperations.h"
2727
#include "dxc/HlslIntrinsicOp.h"
28-
#include "dxc/HLSL/DxilConvergent.h"
2928
#include "dxc/DXIL/DxilResourceProperties.h"
3029

3130
#include "llvm/IR/GetElementPtrTypeIterator.h"
@@ -844,8 +843,8 @@ Value *FindScalarSource(Value *src, unsigned vecIdx = 0) {
844843
vecIdx = (unsigned)cast<ConstantInt>(EE->getIndexOperand())
845844
->getUniqueInteger().getLimitedValue();
846845
src = EE->getVectorOperand();
847-
} else if (hlsl::IsConvergentMarker(src)) {
848-
src = hlsl::GetConvergentSource(src);
846+
} else if (hlsl::dxilutil::IsConvergentMarker(src)) {
847+
src = hlsl::dxilutil::GetConvergentSource(src);
849848
} else {
850849
break; // Found it.
851850
}

lib/Transforms/Utils/Local.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "llvm/Support/raw_ostream.h"
4747

4848
#include "dxc/DXIL/DxilMetadataHelper.h" // HLSL Change - combine dxil metadata.
49+
#include "dxc/DXIL/DxilUtil.h" // HLSL Change - special handling of convergent marker
4950
using namespace llvm;
5051

5152
#define DEBUG_TYPE "local"
@@ -331,6 +332,10 @@ bool llvm::isInstructionTriviallyDead(Instruction *I,
331332
if (Constant *C = dyn_cast<Constant>(CI->getArgOperand(0)))
332333
return C->isNullValue() || isa<UndefValue>(C);
333334

335+
// HLSL change - don't force unused convergenet markers to stay
336+
if (CallInst *CI = dyn_cast<CallInst>(I))
337+
if (hlsl::dxilutil::IsConvergentMarker(CI)) return true;
338+
334339
return false;
335340
}
336341

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %dxc /T ps_6_0 %s | FileCheck %s
2+
3+
// Tests that a convergent marker added by a derivative call
4+
// is removed in time to let the rest of the dead code be removed
5+
6+
7+
// Since everything is ignored, there should be no conditionals
8+
// and just a single return void
9+
10+
// CHECK: void @main
11+
// CHECK-NOT: br
12+
// CHECK: storeOutput
13+
// CHECK: ret void
14+
15+
float main(float d : DEPTH0) : SV_Target {
16+
if (d > 0)
17+
d = max(d, 3.0);
18+
ddx(d+1);
19+
return 0.0;
20+
}

0 commit comments

Comments
 (0)