Skip to content

Commit ffd4fee

Browse files
authored
[Instcombine] Disable bswap match for DXIL (microsoft#5899)
DXIL doesn't support the bswap intrinsic, normally a backend would lower this intrinsic to something else, in this case we don't have a backend so we just end up generating invalid DXIL. The solution here is to disable bswap matching when targeting DXIL. Fixes microsoft#5104
1 parent be4d45b commit ffd4fee

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

include/llvm/ADT/Triple.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,12 @@ class Triple {
509509
getOS() == Triple::PS4;
510510
}
511511

512+
// HLSL Change Begin - Add DXIL Triple.
513+
bool isDXIL() const {
514+
return getArch() == Triple::dxil || getArch() == Triple::dxil64;
515+
}
516+
// HLSL Change End - Add DXIL Triple.
517+
512518
/// @}
513519
/// @name Mutators
514520
/// @{

lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "InstCombineInternal.h"
15+
#include "llvm/ADT/Triple.h" // HLSL Change
1516
#include "llvm/Analysis/InstructionSimplify.h"
1617
#include "llvm/IR/ConstantRange.h"
1718
#include "llvm/IR/Intrinsics.h"
@@ -1634,6 +1635,11 @@ static bool CollectBSwapParts(Value *V, int OverallLeftShift, uint32_t ByteMask,
16341635
/// MatchBSwap - Given an OR instruction, check to see if this is a bswap idiom.
16351636
/// If so, insert the new bswap intrinsic and return it.
16361637
Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) {
1638+
// HLSL Change begin - Disable bswap matching for DXIL.
1639+
Triple T(I.getModule()->getTargetTriple());
1640+
if (T.isDXIL())
1641+
return nullptr;
1642+
// HLSL Change end - Disable bswap matching for DXIL.
16371643
IntegerType *ITy = dyn_cast<IntegerType>(I.getType());
16381644
if (!ITy || ITy->getBitWidth() % 16 ||
16391645
// ByteMask only allows up to 32-byte values.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
; RUN: opt -instcombine -S %s | FileCheck %s
2+
3+
; CHECK-NOT: call i32 @llvm.bswap.i32
4+
target datalayout = "e-m:e-p:32:32-i1:32-i8:32-i16:32-i32:32-i64:64-f16:32-f32:32-f64:64-n8:16:32:64"
5+
target triple = "dxil-ms-dx"
6+
7+
; Function Attrs: nounwind
8+
define i32 @main(i32 %input) #0 {
9+
%1 = shl i32 %input, 24
10+
%2 = and i32 %1, -16777216
11+
%3 = shl i32 %input, 8
12+
%4 = and i32 %3, 16711680
13+
%5 = or i32 %2, %4
14+
%6 = lshr i32 %input, 8
15+
%7 = and i32 %6, 65280
16+
%8 = or i32 %5, %7
17+
%9 = lshr i32 %input, 24
18+
%10 = and i32 %9, 255
19+
%11 = or i32 %8, %10
20+
ret i32 %11
21+
}
22+
23+
attributes #0 = { nounwind }
24+
25+
!llvm.module.flags = !{!0}
26+
!pauseresume = !{!1}
27+
!llvm.ident = !{!2}
28+
29+
!0 = !{i32 2, !"Debug Info Version", i32 3}
30+
!1 = !{!"hlsl-dxilemit", !"hlsl-dxilload"}
31+
!2 = !{!"dxc(private) 1.7.0.14160 (main, adb2dc70fbd-dirty)"}

0 commit comments

Comments
 (0)