Skip to content

Commit 4d08722

Browse files
committed
[DirectX] replace byte splitting via vector bitcast with scalar
instructions - instead of bitcasting and extract element lets use trunc or trunc and logical shift right to split. - fixes #139020
1 parent 8e53e3b commit 4d08722

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

llvm/lib/Target/DirectX/DXILLegalizePass.cpp

Lines changed: 45 additions & 0 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"
@@ -317,6 +319,48 @@ static void removeMemSet(Instruction &I,
317319
ToRemove.push_back(CI);
318320
}
319321

322+
static void
323+
legalizeGetHighLowi64Bytes(Instruction &I,
324+
SmallVectorImpl<Instruction *> &ToRemove,
325+
DenseMap<Value *, Value *> &ReplacedValues) {
326+
if (auto *BitCast = dyn_cast<BitCastInst>(&I)) {
327+
if (BitCast->getDestTy() ==
328+
FixedVectorType::get(Type::getInt32Ty(I.getContext()), 2) &&
329+
BitCast->getSrcTy()->isIntegerTy(64)) {
330+
ToRemove.push_back(BitCast);
331+
ReplacedValues[BitCast] = BitCast->getOperand(0);
332+
}
333+
}
334+
335+
if (auto *Extract = dyn_cast<ExtractElementInst>(&I)) {
336+
auto *VecTy = dyn_cast<FixedVectorType>(Extract->getVectorOperandType());
337+
if (VecTy && VecTy->getElementType()->isIntegerTy(32) &&
338+
VecTy->getNumElements() == 2) {
339+
if (auto *Index = dyn_cast<ConstantInt>(Extract->getIndexOperand())) {
340+
unsigned Idx = Index->getZExtValue();
341+
IRBuilder<> Builder(&I);
342+
assert(dyn_cast<BitCastInst>(Extract->getVectorOperand()));
343+
auto *Replacement = ReplacedValues[Extract->getVectorOperand()];
344+
if (Idx == 0) {
345+
Value *LowBytes = Builder.CreateTrunc(
346+
Replacement, Type::getInt32Ty(I.getContext()));
347+
ReplacedValues[Extract] = LowBytes;
348+
} else {
349+
assert(Idx == 1);
350+
Value *LogicalShiftRight = Builder.CreateLShr(
351+
Replacement,
352+
ConstantInt::get(
353+
Replacement->getType(),
354+
APInt(Replacement->getType()->getIntegerBitWidth(), 32)));
355+
Value *HighBytes = Builder.CreateTrunc(
356+
LogicalShiftRight, Type::getInt32Ty(I.getContext()));
357+
ReplacedValues[Extract] = HighBytes;
358+
}
359+
ToRemove.push_back(Extract);
360+
}
361+
}
362+
}
363+
}
320364
namespace {
321365
class DXILLegalizationPipeline {
322366

@@ -349,6 +393,7 @@ class DXILLegalizationPipeline {
349393
LegalizationPipeline.push_back(downcastI64toI32InsertExtractElements);
350394
LegalizationPipeline.push_back(legalizeFreeze);
351395
LegalizationPipeline.push_back(removeMemSet);
396+
LegalizationPipeline.push_back(legalizeGetHighLowi64Bytes);
352397
}
353398
};
354399

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)