Skip to content

Commit 7cdea51

Browse files
committed
[DirectX][OpLowering] Simplify named struct handling
This removes "replaceFunctionWithNamedStructOp" and folds its functionality into "replaceFunctionWithOp". It turns out we were overcomplicating things and this is trivial to handle generically. Fixes llvm#113192
1 parent b11e1ba commit 7cdea51

File tree

2 files changed

+28
-34
lines changed

2 files changed

+28
-34
lines changed

llvm/lib/Target/DirectX/DXIL.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,7 @@ def MakeDouble : DXILOp<101, makeDouble> {
930930

931931
def SplitDouble : DXILOp<102, splitDouble> {
932932
let Doc = "Splits a double into 2 uints";
933+
let intrinsics = [IntrinSelect<int_dx_splitdouble>];
933934
let arguments = [OverloadTy];
934935
let result = SplitDoubleTy;
935936
let overloads = [Overloads<DXIL1_0, [DoubleTy]>];

llvm/lib/Target/DirectX/DXILOpLowering.cpp

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,28 @@ class OpLowerer {
120120
int Value;
121121
};
122122

123+
/// Replaces uses of a struct with uses of an equivalent named struct.
124+
///
125+
/// DXIL operations that return structs give them well known names, so we need
126+
/// to update uses when we switch from an LLVM intrinsic to an op.
127+
Error replaceNamedStructUses(CallInst *Intrin, CallInst *DXILOp) {
128+
auto *IntrinTy = cast<StructType>(Intrin->getType());
129+
auto *DXILOpTy = cast<StructType>(DXILOp->getType());
130+
if (!IntrinTy->isLayoutIdentical(DXILOpTy))
131+
return make_error<StringError>(
132+
"Type mismatch between intrinsic and DXIL op",
133+
inconvertibleErrorCode());
134+
135+
for (Use &U : make_early_inc_range(Intrin->uses()))
136+
if (auto *EVI = dyn_cast<ExtractValueInst>(U.getUser()))
137+
EVI->setOperand(0, DXILOp);
138+
else
139+
return make_error<StringError>(
140+
"DXIL ops that return structs may only be used by extractvalue",
141+
inconvertibleErrorCode());
142+
return Error::success();
143+
}
144+
123145
[[nodiscard]] bool
124146
replaceFunctionWithOp(Function &F, dxil::OpCode DXILOp,
125147
ArrayRef<IntrinArgSelect> ArgSelects) {
@@ -154,32 +176,13 @@ class OpLowerer {
154176
if (Error E = OpCall.takeError())
155177
return E;
156178

157-
CI->replaceAllUsesWith(*OpCall);
158-
CI->eraseFromParent();
159-
return Error::success();
160-
});
161-
}
162-
163-
[[nodiscard]] bool replaceFunctionWithNamedStructOp(
164-
Function &F, dxil::OpCode DXILOp, Type *NewRetTy,
165-
llvm::function_ref<Error(CallInst *CI, CallInst *Op)> ReplaceUses) {
166-
bool IsVectorArgExpansion = isVectorArgExpansion(F);
167-
return replaceFunction(F, [&](CallInst *CI) -> Error {
168-
SmallVector<Value *> Args;
169-
OpBuilder.getIRB().SetInsertPoint(CI);
170-
if (IsVectorArgExpansion) {
171-
SmallVector<Value *> NewArgs = argVectorFlatten(CI, OpBuilder.getIRB());
172-
Args.append(NewArgs.begin(), NewArgs.end());
179+
if (isa<StructType>(CI->getType())) {
180+
if (Error E = replaceNamedStructUses(CI, *OpCall))
181+
return E;
173182
} else
174-
Args.append(CI->arg_begin(), CI->arg_end());
175-
176-
Expected<CallInst *> OpCall =
177-
OpBuilder.tryCreateOp(DXILOp, Args, CI->getName(), NewRetTy);
178-
if (Error E = OpCall.takeError())
179-
return E;
180-
if (Error E = ReplaceUses(CI, *OpCall))
181-
return E;
183+
CI->replaceAllUsesWith(*OpCall);
182184

185+
CI->eraseFromParent();
183186
return Error::success();
184187
});
185188
}
@@ -814,16 +817,6 @@ class OpLowerer {
814817
case Intrinsic::dx_resource_updatecounter:
815818
HasErrors |= lowerUpdateCounter(F);
816819
break;
817-
// TODO: this can be removed when
818-
// https://github.com/llvm/llvm-project/issues/113192 is fixed
819-
case Intrinsic::dx_splitdouble:
820-
HasErrors |= replaceFunctionWithNamedStructOp(
821-
F, OpCode::SplitDouble,
822-
OpBuilder.getSplitDoubleType(M.getContext()),
823-
[&](CallInst *CI, CallInst *Op) {
824-
return replaceSplitDoubleCallUsages(CI, Op);
825-
});
826-
break;
827820
case Intrinsic::ctpop:
828821
HasErrors |= lowerCtpopToCountBits(F);
829822
break;

0 commit comments

Comments
 (0)