Skip to content

Commit

Permalink
Add load for argument matrix subscript lowering (#3065)
Browse files Browse the repository at this point in the history
Matrix lowering was not handling subscript oeprations when the matrix
was a shader input. copying the matrix to a temporary explicitly or
implicitly as part of a copy in function call worked around the problem.
When the argument copy was eliminated, this problem was exposed.

By adding a load for the indicated matrix for a subscript of a shader
input matrix, the lowering can procede using the lowered matrix from the
load.

Additionally, hull shaders with their uninlineable functions were
failing to detect the constanthull function as a graphics function so
the subscript lowering was being treated as if it were in a library.
Even when it got to signature lowering, there was no support for
lowering matrix loads.

The test for shaderism now tests the function attributes of the module's
entry function. The signature lowering for matrix loads is moved into a
function that is called by input lowering for both the constant hull and
entry functions.

Added general tests for matrix subscripts from different memory types as
well as specific tests for the argument passing problem in pixel and
domain shaders.

A more correct way to identify functions that should delay the lowering
of their matrix parameters to signature lowering time is to query
whether the function has a signature. This covers entry functions for
graphics shaders and also constant patch functions.

Fixes #2958
  • Loading branch information
pow2clk authored Aug 7, 2020
1 parent 55b847a commit 03676eb
Show file tree
Hide file tree
Showing 8 changed files with 320 additions and 91 deletions.
2 changes: 1 addition & 1 deletion lib/HLSL/HLLowerUDT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ void hlsl::ReplaceUsesForLoweredUDT(Value *V, Value *NewV) {

std::vector<Instruction*> DeadInsts;
HLMatrixSubscriptUseReplacer UseReplacer(
CI, NewV, ElemIndices, /*AllowLoweredPtrGEPs*/true, DeadInsts);
CI, NewV, /*TempLoweredMatrix*/nullptr, ElemIndices, /*AllowLoweredPtrGEPs*/true, DeadInsts);
DXASSERT(CI->use_empty(),
"Expected all matrix subscript uses to have been replaced.");
CI->eraseFromParent();
Expand Down
27 changes: 21 additions & 6 deletions lib/HLSL/HLMatrixLowerPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ Value *HLMatrixLowerPass::tryGetLoweredPtrOperand(Value *Ptr, IRBuilder<> &Build
RootPtr = GEP->getPointerOperand();

Argument *Arg = dyn_cast<Argument>(RootPtr);
bool IsNonShaderArg = Arg != nullptr && !m_pHLModule->IsGraphicsShader(Arg->getParent());
bool IsNonShaderArg = Arg != nullptr && !m_pHLModule->IsEntryThatUsesSignatures(Arg->getParent());
if (IsNonShaderArg || isa<AllocaInst>(RootPtr)) {
// Bitcast the matrix pointer to its lowered equivalent.
// The HLMatrixBitcast pass will take care of this later.
Expand Down Expand Up @@ -1474,17 +1474,32 @@ void HLMatrixLowerPass::lowerHLMatSubscript(CallInst *Call, Value *MatPtr, Small

IRBuilder<> CallBuilder(Call);
Value *LoweredPtr = tryGetLoweredPtrOperand(MatPtr, CallBuilder);
if (LoweredPtr == nullptr) return;
Value *LoweredMatrix = nullptr;
Value *RootPtr = LoweredPtr? LoweredPtr: MatPtr;
while (GEPOperator *GEP = dyn_cast<GEPOperator>(RootPtr))
RootPtr = GEP->getPointerOperand();

if (LoweredPtr == nullptr) {
if (!isa<Argument>(RootPtr))
return;

// For a shader input, load the matrix into a lowered ptr
// The load will be handled by LowerSignature
HLMatLoadStoreOpcode Opcode = (HLSubscriptOpcode)GetHLOpcode(Call) == HLSubscriptOpcode::RowMatSubscript ?
HLMatLoadStoreOpcode::RowMatLoad : HLMatLoadStoreOpcode::ColMatLoad;
HLMatrixType MatTy = HLMatrixType::cast(MatPtr->getType()->getPointerElementType());
LoweredMatrix = callHLFunction(
*m_pModule, HLOpcodeGroup::HLMatLoadStore, static_cast<unsigned>(Opcode),
MatTy.getLoweredVectorTypeForReg(), { CallBuilder.getInt32((uint32_t)Opcode), MatPtr },
Call->getCalledFunction()->getAttributes().getFnAttributes(), CallBuilder);
}
// For global variables, we can GEP directly into the lowered vector pointer.
// This is necessary to support group shared memory atomics and the likes.
Value *RootPtr = LoweredPtr;
while (GEPOperator *GEP = dyn_cast<GEPOperator>(RootPtr))
RootPtr = GEP->getPointerOperand();
bool AllowLoweredPtrGEPs = isa<GlobalVariable>(RootPtr);

// Just constructing this does all the work
HLMatrixSubscriptUseReplacer UseReplacer(Call, LoweredPtr, ElemIndices, AllowLoweredPtrGEPs, m_deadInsts);
HLMatrixSubscriptUseReplacer UseReplacer(Call, LoweredPtr, LoweredMatrix,
ElemIndices, AllowLoweredPtrGEPs, m_deadInsts);

DXASSERT(Call->use_empty(), "Expected all matrix subscript uses to have been replaced.");
addToDeadInsts(Call);
Expand Down
18 changes: 12 additions & 6 deletions lib/HLSL/HLMatrixSubscriptUseReplacer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
using namespace llvm;
using namespace hlsl;

HLMatrixSubscriptUseReplacer::HLMatrixSubscriptUseReplacer(CallInst* Call, Value *LoweredPtr,
HLMatrixSubscriptUseReplacer::HLMatrixSubscriptUseReplacer(CallInst* Call, Value *LoweredPtr, Value *TempLoweredMatrix,
SmallVectorImpl<Value*> &ElemIndices, bool AllowLoweredPtrGEPs, std::vector<Instruction*> &DeadInsts)
: LoweredPtr(LoweredPtr), ElemIndices(ElemIndices), DeadInsts(DeadInsts), AllowLoweredPtrGEPs(AllowLoweredPtrGEPs)
: LoweredPtr(LoweredPtr), ElemIndices(ElemIndices), DeadInsts(DeadInsts),
AllowLoweredPtrGEPs(AllowLoweredPtrGEPs), TempLoweredMatrix(TempLoweredMatrix)
{
HasScalarResult = !Call->getType()->getPointerElementType()->isVectorTy();

Expand All @@ -32,6 +33,11 @@ HLMatrixSubscriptUseReplacer::HLMatrixSubscriptUseReplacer(CallInst* Call, Value
}
}

if (TempLoweredMatrix)
LoweredTy = TempLoweredMatrix->getType();
else
LoweredTy = LoweredPtr->getType()->getPointerElementType();

replaceUses(Call, /* GEPIdx */ nullptr);
}

Expand Down Expand Up @@ -162,7 +168,8 @@ void HLMatrixSubscriptUseReplacer::cacheLoweredMatrix(bool ForDynamicIndexing, I

// Load without memory to register representation conversion,
// since the point is to mimic pointer semantics
TempLoweredMatrix = Builder.CreateLoad(LoweredPtr);
if (!TempLoweredMatrix)
TempLoweredMatrix = Builder.CreateLoad(LoweredPtr);

if (!ForDynamicIndexing) return;

Expand Down Expand Up @@ -238,7 +245,6 @@ Value *HLMatrixSubscriptUseReplacer::loadVector(IRBuilder<> &Builder) {

// Otherwise load elements one by one
// Lowered form may be array when AllowLoweredPtrGEPs == true.
Type* LoweredTy = LoweredPtr->getType()->getPointerElementType();
Type* ElemTy = LoweredTy->isVectorTy() ? LoweredTy->getScalarType() :
cast<ArrayType>(LoweredTy)->getArrayElementType();
VectorType *VecTy = VectorType::get(ElemTy, static_cast<unsigned>(ElemIndices.size()));
Expand Down Expand Up @@ -270,7 +276,7 @@ void HLMatrixSubscriptUseReplacer::flushLoweredMatrix(IRBuilder<> &Builder) {
// First re-create the vector from the temporary array
DXASSERT_NOMSG(LazyTempElemArrayAlloca != nullptr);

VectorType *LoweredMatrixTy = cast<VectorType>(LoweredPtr->getType()->getPointerElementType());
VectorType *LoweredMatrixTy = cast<VectorType>(LoweredTy);
TempLoweredMatrix = UndefValue::get(LoweredMatrixTy);
Value *GEPIndices[2] = { Builder.getInt32(0), nullptr };
for (unsigned ElemIdx = 0; ElemIdx < LoweredMatrixTy->getNumElements(); ++ElemIdx) {
Expand All @@ -284,4 +290,4 @@ void HLMatrixSubscriptUseReplacer::flushLoweredMatrix(IRBuilder<> &Builder) {
// Store back the lowered matrix to its pointer
Builder.CreateStore(TempLoweredMatrix, LoweredPtr);
TempLoweredMatrix = nullptr;
}
}
5 changes: 3 additions & 2 deletions lib/HLSL/HLMatrixSubscriptUseReplacer.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace hlsl {
class HLMatrixSubscriptUseReplacer {
public:
// The constructor does everything
HLMatrixSubscriptUseReplacer(llvm::CallInst* Call, llvm::Value *LoweredPtr,
HLMatrixSubscriptUseReplacer(llvm::CallInst* Call, llvm::Value *LoweredPtr, llvm::Value *TempLoweredMatrix,
llvm::SmallVectorImpl<llvm::Value*> &ElemIndices, bool AllowLoweredPtrGEPs,
std::vector<llvm::Instruction*> &DeadInsts);

Expand All @@ -51,6 +51,7 @@ class HLMatrixSubscriptUseReplacer {
bool AllowLoweredPtrGEPs = false;
bool HasScalarResult = false;
bool HasDynamicElemIndex = false;
llvm::Type *LoweredTy = nullptr;

// The entire lowered matrix as loaded from LoweredPtr,
// nullptr if we copied it to a temporary array.
Expand All @@ -64,4 +65,4 @@ class HLMatrixSubscriptUseReplacer {
// so we can dynamically index the level 1 indices.
llvm::AllocaInst *LazyTempElemIndicesArrayAlloca = nullptr;
};
} // namespace hlsl
} // namespace hlsl
188 changes: 112 additions & 76 deletions lib/HLSL/HLSignatureLower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,7 @@ Value *GenerateLdInput(Function *loadInput, ArrayRef<Value *> args,
}
}


Value *replaceLdWithLdInput(Function *loadInput, LoadInst *ldInst,
unsigned cols, MutableArrayRef<Value *> args,
bool bCast) {
Expand Down Expand Up @@ -654,6 +655,96 @@ Value *replaceLdWithLdInput(Function *loadInput, LoadInst *ldInst,
}
}


void replaceMatStWithStOutputs(CallInst *CI, HLMatLoadStoreOpcode matOp,
Function *ldStFunc, Constant *OpArg, Constant *ID,
Constant *columnConsts[],Value *vertexOrPrimID,
Value *idxVal) {
IRBuilder<> LocalBuilder(CI);
Value *Val = CI->getArgOperand(HLOperandIndex::kMatStoreValOpIdx);
HLMatrixType MatTy = HLMatrixType::cast(
CI->getArgOperand(HLOperandIndex::kMatStoreDstPtrOpIdx)
->getType()->getPointerElementType());

Val = MatTy.emitLoweredRegToMem(Val, LocalBuilder);

if (matOp == HLMatLoadStoreOpcode::ColMatStore) {
for (unsigned c = 0; c < MatTy.getNumColumns(); c++) {
Constant *constColIdx = LocalBuilder.getInt32(c);
Value *colIdx = LocalBuilder.CreateAdd(idxVal, constColIdx);

for (unsigned r = 0; r < MatTy.getNumRows(); r++) {
unsigned matIdx = MatTy.getColumnMajorIndex(r, c);
Value *Elt = LocalBuilder.CreateExtractElement(Val, matIdx);
LocalBuilder.CreateCall(ldStFunc,
{ OpArg, ID, colIdx, columnConsts[r], Elt });
}
}
} else {
for (unsigned r = 0; r < MatTy.getNumRows(); r++) {
Constant *constRowIdx = LocalBuilder.getInt32(r);
Value *rowIdx = LocalBuilder.CreateAdd(idxVal, constRowIdx);
for (unsigned c = 0; c < MatTy.getNumColumns(); c++) {
unsigned matIdx = MatTy.getRowMajorIndex(r, c);
Value *Elt = LocalBuilder.CreateExtractElement(Val, matIdx);
LocalBuilder.CreateCall(ldStFunc,
{ OpArg, ID, rowIdx, columnConsts[c], Elt });
}
}
}
CI->eraseFromParent();
}


void replaceMatLdWithLdInputs(CallInst *CI, HLMatLoadStoreOpcode matOp,
Function *ldStFunc, Constant *OpArg, Constant *ID,
Constant *columnConsts[],Value *vertexOrPrimID,
Value *idxVal) {
IRBuilder<> LocalBuilder(CI);
HLMatrixType MatTy = HLMatrixType::cast(
CI->getArgOperand(HLOperandIndex::kMatLoadPtrOpIdx)
->getType()->getPointerElementType());
std::vector<Value *> matElts(MatTy.getNumElements());

if (matOp == HLMatLoadStoreOpcode::ColMatLoad) {
for (unsigned c = 0; c < MatTy.getNumColumns(); c++) {
Constant *constRowIdx = LocalBuilder.getInt32(c);
Value *rowIdx = LocalBuilder.CreateAdd(idxVal, constRowIdx);
for (unsigned r = 0; r < MatTy.getNumRows(); r++) {
SmallVector<Value *, 4> args = { OpArg, ID, rowIdx, columnConsts[r] };
if (vertexOrPrimID)
args.emplace_back(vertexOrPrimID);

Value *input = LocalBuilder.CreateCall(ldStFunc, args);
unsigned matIdx = MatTy.getColumnMajorIndex(r, c);
matElts[matIdx] = input;
}
}
} else {
for (unsigned r = 0; r < MatTy.getNumRows(); r++) {
Constant *constRowIdx = LocalBuilder.getInt32(r);
Value *rowIdx = LocalBuilder.CreateAdd(idxVal, constRowIdx);
for (unsigned c = 0; c < MatTy.getNumColumns(); c++) {
SmallVector<Value *, 4> args = { OpArg, ID, rowIdx, columnConsts[c] };
if (vertexOrPrimID)
args.emplace_back(vertexOrPrimID);

Value *input = LocalBuilder.CreateCall(ldStFunc, args);
unsigned matIdx = MatTy.getRowMajorIndex(r, c);
matElts[matIdx] = input;
}
}
}

Value *newVec =
HLMatrixLower::BuildVector(matElts[0]->getType(), matElts, LocalBuilder);
newVec = MatTy.emitLoweredMemToReg(newVec, LocalBuilder);

CI->replaceAllUsesWith(newVec);
CI->eraseFromParent();
}


void replaceDirectInputParameter(Value *param, Function *loadInput,
unsigned cols, MutableArrayRef<Value *> args,
bool bCast, OP *hlslOP, IRBuilder<> &Builder) {
Expand Down Expand Up @@ -964,84 +1055,11 @@ void GenerateInputOutputUserCall(InputOutputAccessInfo &info, Value *undefVertex
switch (matOp) {
case HLMatLoadStoreOpcode::ColMatLoad:
case HLMatLoadStoreOpcode::RowMatLoad: {
IRBuilder<> LocalBuilder(CI);
HLMatrixType MatTy = HLMatrixType::cast(
CI->getArgOperand(HLOperandIndex::kMatLoadPtrOpIdx)
->getType()->getPointerElementType());
std::vector<Value *> matElts(MatTy.getNumElements());

if (matOp == HLMatLoadStoreOpcode::ColMatLoad) {
for (unsigned c = 0; c < MatTy.getNumColumns(); c++) {
Constant *constRowIdx = LocalBuilder.getInt32(c);
Value *rowIdx = LocalBuilder.CreateAdd(idxVal, constRowIdx);
for (unsigned r = 0; r < MatTy.getNumRows(); r++) {
SmallVector<Value *, 4> args = { OpArg, ID, rowIdx, columnConsts[r] };
if (vertexOrPrimID)
args.emplace_back(vertexOrPrimID);

Value *input = LocalBuilder.CreateCall(ldStFunc, args);
unsigned matIdx = MatTy.getColumnMajorIndex(r, c);
matElts[matIdx] = input;
}
}
} else {
for (unsigned r = 0; r < MatTy.getNumRows(); r++) {
Constant *constRowIdx = LocalBuilder.getInt32(r);
Value *rowIdx = LocalBuilder.CreateAdd(idxVal, constRowIdx);
for (unsigned c = 0; c < MatTy.getNumColumns(); c++) {
SmallVector<Value *, 4> args = { OpArg, ID, rowIdx, columnConsts[c] };
if (vertexOrPrimID)
args.emplace_back(vertexOrPrimID);

Value *input = LocalBuilder.CreateCall(ldStFunc, args);
unsigned matIdx = MatTy.getRowMajorIndex(r, c);
matElts[matIdx] = input;
}
}
}

Value *newVec =
HLMatrixLower::BuildVector(matElts[0]->getType(), matElts, LocalBuilder);
newVec = MatTy.emitLoweredMemToReg(newVec, LocalBuilder);

CI->replaceAllUsesWith(newVec);
CI->eraseFromParent();
replaceMatLdWithLdInputs(CI, matOp, ldStFunc, OpArg, ID, columnConsts, vertexOrPrimID, idxVal);
} break;
case HLMatLoadStoreOpcode::ColMatStore:
case HLMatLoadStoreOpcode::RowMatStore: {
IRBuilder<> LocalBuilder(CI);
Value *Val = CI->getArgOperand(HLOperandIndex::kMatStoreValOpIdx);
HLMatrixType MatTy = HLMatrixType::cast(
CI->getArgOperand(HLOperandIndex::kMatStoreDstPtrOpIdx)
->getType()->getPointerElementType());

Val = MatTy.emitLoweredRegToMem(Val, LocalBuilder);

if (matOp == HLMatLoadStoreOpcode::ColMatStore) {
for (unsigned c = 0; c < MatTy.getNumColumns(); c++) {
Constant *constColIdx = LocalBuilder.getInt32(c);
Value *colIdx = LocalBuilder.CreateAdd(idxVal, constColIdx);

for (unsigned r = 0; r < MatTy.getNumRows(); r++) {
unsigned matIdx = MatTy.getColumnMajorIndex(r, c);
Value *Elt = LocalBuilder.CreateExtractElement(Val, matIdx);
LocalBuilder.CreateCall(ldStFunc,
{ OpArg, ID, colIdx, columnConsts[r], Elt });
}
}
} else {
for (unsigned r = 0; r < MatTy.getNumRows(); r++) {
Constant *constRowIdx = LocalBuilder.getInt32(r);
Value *rowIdx = LocalBuilder.CreateAdd(idxVal, constRowIdx);
for (unsigned c = 0; c < MatTy.getNumColumns(); c++) {
unsigned matIdx = MatTy.getRowMajorIndex(r, c);
Value *Elt = LocalBuilder.CreateExtractElement(Val, matIdx);
LocalBuilder.CreateCall(ldStFunc,
{ OpArg, ID, rowIdx, columnConsts[c], Elt });
}
}
}
CI->eraseFromParent();
replaceMatStWithStOutputs(CI, matOp, ldStFunc, OpArg, ID, columnConsts, vertexOrPrimID, idxVal);
} break;
}
} else {
Expand Down Expand Up @@ -1386,6 +1404,14 @@ void HLSignatureLower::GenerateDxilPatchConstantFunctionInputs() {
Type *i1Ty = Type::getInt1Ty(constZero->getContext());
Type *i32Ty = constZero->getType();

Constant *columnConsts[] = {
hlslOP->GetU8Const(0), hlslOP->GetU8Const(1), hlslOP->GetU8Const(2),
hlslOP->GetU8Const(3), hlslOP->GetU8Const(4), hlslOP->GetU8Const(5),
hlslOP->GetU8Const(6), hlslOP->GetU8Const(7), hlslOP->GetU8Const(8),
hlslOP->GetU8Const(9), hlslOP->GetU8Const(10), hlslOP->GetU8Const(11),
hlslOP->GetU8Const(12), hlslOP->GetU8Const(13), hlslOP->GetU8Const(14),
hlslOP->GetU8Const(15)};

for (Argument &arg : patchConstantFunc->args()) {
DxilParameterAnnotation &paramAnnotation =
patchFuncAnnotation->GetParameterAnnotation(arg.getArgNo());
Expand Down Expand Up @@ -1422,11 +1448,21 @@ void HLSignatureLower::GenerateDxilPatchConstantFunctionInputs() {
collectInputOutputAccessInfo(&arg, constZero, accessInfoList,
/*hasVertexOrPrimID*/ true, true, bRowMajor, false);
for (InputOutputAccessInfo &info : accessInfoList) {
Constant *OpArg = hlslOP->GetU32Const((unsigned)opcode);
if (LoadInst *ldInst = dyn_cast<LoadInst>(info.user)) {
Constant *OpArg = hlslOP->GetU32Const((unsigned)opcode);
Value *args[] = {OpArg, inputID, info.idx, info.vectorIdx,
info.vertexOrPrimID};
replaceLdWithLdInput(dxilLdFunc, ldInst, cols, args, bI1Cast);
} else if (CallInst *CI = dyn_cast<CallInst>(info.user)) {
HLOpcodeGroup group = GetHLOpcodeGroupByName(CI->getCalledFunction());
// Intrinsic will be translated later.
if (group == HLOpcodeGroup::HLIntrinsic || group == HLOpcodeGroup::NotHL)
return;
unsigned opcode = GetHLOpcode(CI);
DXASSERT_NOMSG(group == HLOpcodeGroup::HLMatLoadStore);
HLMatLoadStoreOpcode matOp = static_cast<HLMatLoadStoreOpcode>(opcode);
if (matOp == HLMatLoadStoreOpcode::ColMatLoad || matOp == HLMatLoadStoreOpcode::RowMatLoad)
replaceMatLdWithLdInputs(CI, matOp, dxilLdFunc, OpArg, inputID, columnConsts, info.vertexOrPrimID, info.idx);
} else {
DXASSERT(0, "input should only be ld");
}
Expand Down
Loading

0 comments on commit 03676eb

Please sign in to comment.