Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add load for argument matrix subscript lowering #3065

Merged
merged 4 commits into from
Aug 7, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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->IsGraphicsShader(m_pHLModule->GetEntryFunction());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use IsEntryThatUsesSignatures(Arg->getParent()) instead.

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();
}


Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note that these are just moved to subfunctions without alteration to the code.

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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving this code wasn't strictly necessary as it's still only called from here, but I moved it for consistency with the loading replacement code. I think it makes the control flow here easier to read too.

} 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