Skip to content

Commit c1e0fae

Browse files
authored
[DirectX] replace byte splitting via vector bitcast with scalar (#140167)
instructions - instead of bitcasting and extract element lets use trunc or trunc and logical shift right to split. - fixes #139020
1 parent 9cd5378 commit c1e0fae

File tree

2 files changed

+102
-17
lines changed

2 files changed

+102
-17
lines changed

llvm/lib/Target/DirectX/DXILLegalizePass.cpp

Lines changed: 84 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include "DXILLegalizePass.h"
1010
#include "DirectX.h"
11+
#include "llvm/ADT/APInt.h"
12+
#include "llvm/IR/Constants.h"
1113
#include "llvm/IR/Function.h"
1214
#include "llvm/IR/IRBuilder.h"
1315
#include "llvm/IR/InstIterator.h"
@@ -510,40 +512,105 @@ static void updateFnegToFsub(Instruction &I,
510512
ToRemove.push_back(&I);
511513
}
512514

515+
static void
516+
legalizeGetHighLowi64Bytes(Instruction &I,
517+
SmallVectorImpl<Instruction *> &ToRemove,
518+
DenseMap<Value *, Value *> &ReplacedValues) {
519+
if (auto *BitCast = dyn_cast<BitCastInst>(&I)) {
520+
if (BitCast->getDestTy() ==
521+
FixedVectorType::get(Type::getInt32Ty(I.getContext()), 2) &&
522+
BitCast->getSrcTy()->isIntegerTy(64)) {
523+
ToRemove.push_back(BitCast);
524+
ReplacedValues[BitCast] = BitCast->getOperand(0);
525+
return;
526+
}
527+
}
528+
529+
if (auto *Extract = dyn_cast<ExtractElementInst>(&I)) {
530+
if (!dyn_cast<BitCastInst>(Extract->getVectorOperand()))
531+
return;
532+
auto *VecTy = dyn_cast<FixedVectorType>(Extract->getVectorOperandType());
533+
if (VecTy && VecTy->getElementType()->isIntegerTy(32) &&
534+
VecTy->getNumElements() == 2) {
535+
if (auto *Index = dyn_cast<ConstantInt>(Extract->getIndexOperand())) {
536+
unsigned Idx = Index->getZExtValue();
537+
IRBuilder<> Builder(&I);
538+
539+
auto *Replacement = ReplacedValues[Extract->getVectorOperand()];
540+
assert(Replacement && "The BitCast replacement should have been set "
541+
"before working on ExtractElementInst.");
542+
if (Idx == 0) {
543+
Value *LowBytes = Builder.CreateTrunc(
544+
Replacement, Type::getInt32Ty(I.getContext()));
545+
ReplacedValues[Extract] = LowBytes;
546+
} else {
547+
assert(Idx == 1);
548+
Value *LogicalShiftRight = Builder.CreateLShr(
549+
Replacement,
550+
ConstantInt::get(
551+
Replacement->getType(),
552+
APInt(Replacement->getType()->getIntegerBitWidth(), 32)));
553+
Value *HighBytes = Builder.CreateTrunc(
554+
LogicalShiftRight, Type::getInt32Ty(I.getContext()));
555+
ReplacedValues[Extract] = HighBytes;
556+
}
557+
ToRemove.push_back(Extract);
558+
Extract->replaceAllUsesWith(ReplacedValues[Extract]);
559+
}
560+
}
561+
}
562+
}
563+
513564
namespace {
514565
class DXILLegalizationPipeline {
515566

516567
public:
517568
DXILLegalizationPipeline() { initializeLegalizationPipeline(); }
518569

519570
bool runLegalizationPipeline(Function &F) {
571+
bool MadeChange = false;
520572
SmallVector<Instruction *> ToRemove;
521573
DenseMap<Value *, Value *> ReplacedValues;
522-
for (auto &I : instructions(F)) {
523-
for (auto &LegalizationFn : LegalizationPipeline)
524-
LegalizationFn(I, ToRemove, ReplacedValues);
525-
}
574+
for (int Stage = 0; Stage < NumStages; ++Stage) {
575+
ToRemove.clear();
576+
ReplacedValues.clear();
577+
for (auto &I : instructions(F)) {
578+
for (auto &LegalizationFn : LegalizationPipeline[Stage])
579+
LegalizationFn(I, ToRemove, ReplacedValues);
580+
}
526581

527-
for (auto *Inst : reverse(ToRemove))
528-
Inst->eraseFromParent();
582+
for (auto *Inst : reverse(ToRemove))
583+
Inst->eraseFromParent();
529584

530-
return !ToRemove.empty();
585+
MadeChange |= !ToRemove.empty();
586+
}
587+
return MadeChange;
531588
}
532589

533590
private:
534-
SmallVector<
591+
enum LegalizationStage { Stage1 = 0, Stage2 = 1, NumStages };
592+
593+
using LegalizationFnTy =
535594
std::function<void(Instruction &, SmallVectorImpl<Instruction *> &,
536-
DenseMap<Value *, Value *> &)>>
537-
LegalizationPipeline;
595+
DenseMap<Value *, Value *> &)>;
596+
597+
SmallVector<LegalizationFnTy> LegalizationPipeline[NumStages];
538598

539599
void initializeLegalizationPipeline() {
540-
LegalizationPipeline.push_back(upcastI8AllocasAndUses);
541-
LegalizationPipeline.push_back(fixI8UseChain);
542-
LegalizationPipeline.push_back(downcastI64toI32InsertExtractElements);
543-
LegalizationPipeline.push_back(legalizeFreeze);
544-
LegalizationPipeline.push_back(legalizeMemCpy);
545-
LegalizationPipeline.push_back(removeMemSet);
546-
LegalizationPipeline.push_back(updateFnegToFsub);
600+
LegalizationPipeline[Stage1].push_back(upcastI8AllocasAndUses);
601+
LegalizationPipeline[Stage1].push_back(fixI8UseChain);
602+
LegalizationPipeline[Stage1].push_back(legalizeGetHighLowi64Bytes);
603+
LegalizationPipeline[Stage1].push_back(legalizeFreeze);
604+
LegalizationPipeline[Stage1].push_back(legalizeMemCpy);
605+
LegalizationPipeline[Stage1].push_back(removeMemSet);
606+
LegalizationPipeline[Stage1].push_back(updateFnegToFsub);
607+
// Note: legalizeGetHighLowi64Bytes and
608+
// downcastI64toI32InsertExtractElements both modify extractelement, so they
609+
// must run staggered stages. legalizeGetHighLowi64Bytes runs first b\c it
610+
// removes extractelements, reducing the number that
611+
// downcastI64toI32InsertExtractElements needs to handle.
612+
LegalizationPipeline[Stage2].push_back(
613+
downcastI64toI32InsertExtractElements);
547614
}
548615
};
549616

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -S -passes='dxil-legalize' -mtriple=dxil-pc-shadermodel6.3-library %s | FileCheck %s
3+
4+
define void @split_via_extract(i64 noundef %a) {
5+
; CHECK-LABEL: define void @split_via_extract(
6+
; CHECK-SAME: i64 noundef [[A:%.*]]) {
7+
; CHECK-NEXT: [[ENTRY:.*:]]
8+
; CHECK-NEXT: [[TMP0:%.*]] = trunc i64 [[A]] to i32
9+
; CHECK-NEXT: [[TMP1:%.*]] = lshr i64 [[A]], 32
10+
; CHECK-NEXT: [[TMP2:%.*]] = trunc i64 [[TMP1]] to i32
11+
; CHECK-NEXT: ret void
12+
;
13+
entry:
14+
%vecA = bitcast i64 %a to <2 x i32>
15+
%low = extractelement <2 x i32> %vecA, i32 0 ; low 32 bits
16+
%high = extractelement <2 x i32> %vecA, i32 1 ; high 32 bits
17+
ret void
18+
}

0 commit comments

Comments
 (0)