Skip to content

Commit b42e4d2

Browse files
amielczaigcbot
authored andcommitted
Expand MathExtras wrapper
Expand the MathExtras wrapper to cover methods removed in LLVM17.
1 parent 28b8d61 commit b42e4d2

File tree

15 files changed

+72
-38
lines changed

15 files changed

+72
-38
lines changed

IGC/Compiler/CISACodeGen/EmitVISAPass.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ SPDX-License-Identifier: MIT
3636
#include "llvmWrapper/IR/Instructions.h"
3737
#include "llvmWrapper/IR/DerivedTypes.h"
3838
#include "llvmWrapper/IR/Function.h"
39+
#include "llvmWrapper/Support/MathExtras.h"
3940
#include "llvm/Support/CommandLine.h"
4041
#include "llvm/Support/Path.h"
4142
#include "llvm/Support/FormattedStream.h"
@@ -2587,7 +2588,7 @@ void EmitPass::emitMayUnalignedVectorCopy(CVariable *Dst, uint32_t Dst_off, CVar
25872588
// This alignment must be aligned for both Dst and Src. The max align
25882589
// starts with element size (tyBytes) and decreases.
25892590
uint32_t remainingBytes = eltBytes - off;
2590-
uint32_t maxAlign = (uint32_t)PowerOf2Floor(remainingBytes);
2591+
uint32_t maxAlign = (uint32_t)IGCLLVM::bit_floor(remainingBytes);
25912592
uint32_t currAlign = (uint32_t)MinAlign(maxAlign, doff);
25922593
currAlign = (uint32_t)MinAlign(currAlign, soff);
25932594

@@ -18848,7 +18849,7 @@ void EmitPass::emitUniformVectorCopy(CVariable *Dst, CVariable *Src, uint32_t nE
1884818849
// Start with the max execution size that is legal for the vector element
1884918850
// and is no greater than the current simdsize.
1885018851
uint32_t maxNumElts = (2 * getGRFSize()) / Dst->GetElemSize();
18851-
uint32_t maxSimd = std::min(width, (uint32_t)PowerOf2Floor(maxNumElts));
18852+
uint32_t maxSimd = std::min(width, (uint32_t)IGCLLVM::bit_floor(maxNumElts));
1885218853
if (maxSimd == 32 && allowLargerSIMDSize) {
1885318854
while (partialCopy(SIMDMode::SIMD32))
1885418855
;

IGC/Compiler/CISACodeGen/LdShrink.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ SPDX-License-Identifier: MIT
1010
#include <llvm/Pass.h>
1111
#include <llvm/IR/DataLayout.h>
1212
#include <llvmWrapper/Support/Alignment.h>
13-
#include <llvm/Support/MathExtras.h>
13+
#include <llvmWrapper/Support/MathExtras.h>
1414
#include <llvmWrapper/IR/DerivedTypes.h>
1515
#include "common/LLVMWarningsPop.hpp"
1616
#include "Compiler/CISACodeGen/ShaderCodeGen.hpp"
@@ -116,8 +116,8 @@ bool LdShrink::runOnFunction(Function &F) {
116116
continue;
117117
if (!isShiftedMask_32(Mask))
118118
continue;
119-
unsigned Offset = llvm::countTrailingZeros(Mask);
120-
unsigned Length = llvm::countTrailingZeros((Mask >> Offset) + 1);
119+
unsigned Offset = IGCLLVM::countr_zero(Mask);
120+
unsigned Length = IGCLLVM::countr_zero((Mask >> Offset) + 1);
121121
// TODO: So far skip narrow vector.
122122
if (Length != 1)
123123
continue;

IGC/Compiler/Optimizer/OpenCLPasses/AlignmentAnalysis/AlignmentAnalysis.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ SPDX-License-Identifier: MIT
1111
#include "Compiler/CodeGenPublic.h"
1212
#include "common/LLVMWarningsPush.hpp"
1313
#include "llvm/IR/InstIterator.h"
14-
#include "llvm/Support/MathExtras.h"
14+
#include "llvmWrapper/Support/MathExtras.h"
1515
#include "llvm/IR/GetElementPtrTypeIterator.h"
1616
#include "llvm/IR/DiagnosticInfo.h"
1717
#include "llvmWrapper/Support/Alignment.h"
@@ -175,7 +175,7 @@ Align AlignmentAnalysis::getConstantAlignment(uint64_t C) const {
175175
return Align(Value::MaximumAlignment);
176176
}
177177

178-
return std::min(Align(Value::MaximumAlignment), Align(1ULL << llvm::countTrailingZeros(C)));
178+
return std::min(Align(Value::MaximumAlignment), Align(1ULL << IGCLLVM::countr_zero(C)));
179179
}
180180

181181
Align AlignmentAnalysis::getAlignValue(Value *V) const {

IGC/VectorCompiler/include/GenXUtil.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ SPDX-License-Identifier: MIT
2727

2828
#include "llvmWrapper/IR/DerivedTypes.h"
2929
#include <llvmWrapper/ADT/Optional.h>
30+
#include "llvmWrapper/Support/MathExtras.h"
3031

3132
#include "Probe/Assertion.h"
3233

@@ -47,7 +48,7 @@ class Bale;
4748
// Utility function to get the integral log base 2 of an integer, or -1 if
4849
// the input is not a power of 2.
4950
inline int exactLog2(unsigned Val) {
50-
unsigned CLZ = llvm::countLeadingZeros(Val);
51+
unsigned CLZ = IGCLLVM::countl_zero(Val);
5152
if (CLZ != 32 && 1U << (31 - CLZ) == Val)
5253
return 31 - CLZ;
5354
return -1;
@@ -58,7 +59,7 @@ inline int exactLog2(unsigned Val) {
5859
template <typename T> inline int log2(T Val) {
5960
if (Val <= 0)
6061
return -1;
61-
unsigned CLZ = llvm::countLeadingZeros<uint32_t>(Val);
62+
unsigned CLZ = IGCLLVM::countl_zero<uint32_t>(Val);
6263
IGC_ASSERT_EXIT(CLZ < 32);
6364
return 31 - CLZ;
6465
}

IGC/VectorCompiler/lib/GenXCodeGen/GenXAlignmentInfo.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ SPDX-License-Identifier: MIT
3030
#include "llvm/IR/Instructions.h"
3131
#include "llvm/IR/Intrinsics.h"
3232
#include "llvm/Support/Debug.h"
33-
33+
#include "llvmWrapper/Support/MathExtras.h"
3434
#include "Probe/Assertion.h"
3535

3636
#define DEBUG_TYPE "GENX_ALIGNMENT_INFO"
@@ -286,7 +286,7 @@ Alignment AlignmentInfo::get(Value *V) {
286286
* Alignment constructor given literal value
287287
*/
288288
Alignment::Alignment(unsigned C) {
289-
LogAlign = C ? countTrailingZeros(C) : 31;
289+
LogAlign = C ? IGCLLVM::countr_zero(C) : 31;
290290
ExtraBits = 0;
291291
ConstBits = (C < MaskForUnknown) ? C : MaskForUnknown;
292292
}
@@ -303,7 +303,7 @@ Alignment Alignment::getAlignmentForConstant(Constant *C) {
303303
int64_t SVal = CI->getSExtValue();
304304
// Get least significant bits to count LogAlign
305305
unsigned LSBBits = SVal & UnsignedAllOnes;
306-
A.LogAlign = LSBBits ? countTrailingZeros(LSBBits) : 31;
306+
A.LogAlign = LSBBits ? IGCLLVM::countr_zero(LSBBits) : 31;
307307

308308
A.ExtraBits = 0;
309309
A.ConstBits = MaskForUnknown;
@@ -348,8 +348,8 @@ Alignment Alignment::merge(Alignment Other) const {
348348
if (MinLogAlign) {
349349
unsigned DisagreeExtraBits =
350350
(ExtraBits ^ Other.ExtraBits) & ((1 << MinLogAlign) - 1);
351-
MinLogAlign = std::min(
352-
MinLogAlign, (unsigned)llvm::countTrailingZeros(DisagreeExtraBits));
351+
MinLogAlign = std::min(MinLogAlign,
352+
(unsigned)IGCLLVM::countr_zero(DisagreeExtraBits));
353353
}
354354
IGC_ASSERT_EXIT(MinLogAlign < 32);
355355
return Alignment(MinLogAlign, ExtraBits & ((1 << MinLogAlign) - 1));
@@ -367,7 +367,7 @@ Alignment Alignment::add(Alignment Other) const {
367367
if (MinLogAlign) {
368368
ExtraBits2 = (ExtraBits + Other.ExtraBits) & ((1 << MinLogAlign) - 1);
369369
MinLogAlign =
370-
std::min(MinLogAlign, (unsigned)llvm::countTrailingZeros(ExtraBits2));
370+
std::min(MinLogAlign, (unsigned)IGCLLVM::countr_zero(ExtraBits2));
371371
}
372372
IGC_ASSERT_EXIT(MinLogAlign < 32);
373373
return Alignment(MinLogAlign, ExtraBits2 & ((1 << MinLogAlign) - 1));
@@ -391,7 +391,7 @@ Alignment Alignment::mul(Alignment Other) const {
391391
if (MinLogAlign) {
392392
ExtraBits2 = (ExtraBits * Other.ExtraBits) & ((1 << MinLogAlign) - 1);
393393
MinLogAlign =
394-
std::min(MinLogAlign, (unsigned)llvm::countTrailingZeros(ExtraBits2));
394+
std::min(MinLogAlign, (unsigned)IGCLLVM::countr_zero(ExtraBits2));
395395
}
396396
IGC_ASSERT_EXIT(MinLogAlign < 32);
397397
return Alignment(MinLogAlign, ExtraBits2 & ((1 << MinLogAlign) - 1));
@@ -410,7 +410,7 @@ Alignment Alignment::logicalOp(ConstantInt *CI, SelectFunction F) const {
410410
Val > std::numeric_limits<int>::max())
411411
return Alignment::getUnknown();
412412
unsigned UVal = static_cast<unsigned>(std::abs(Val));
413-
unsigned ValLSB = llvm::countTrailingZeros(UVal);
413+
unsigned ValLSB = IGCLLVM::countr_zero(UVal);
414414
// Chop off constant bits according to log align
415415
unsigned NewLogAlign = F(ValLSB, LogAlign);
416416
IGC_ASSERT_EXIT(NewLogAlign < 32);

IGC/VectorCompiler/lib/GenXCodeGen/GenXConstants.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,8 +1301,8 @@ unsigned ConstantLoader::getRegionBits(unsigned NeededBits,
13011301
if (!NeededBits)
13021302
return 0;
13031303
// Get the first and last element numbers in NeededBits.
1304-
unsigned FirstNeeded = llvm::countTrailingZeros(NeededBits);
1305-
unsigned LastNeeded = 31 - llvm::countLeadingZeros((uint32_t)NeededBits);
1304+
unsigned FirstNeeded = IGCLLVM::countr_zero(NeededBits);
1305+
unsigned LastNeeded = 31 - IGCLLVM::countl_zero((uint32_t)NeededBits);
13061306
// Set the max width to the min size including both those elements
13071307
// rounded up to the next power of two.
13081308
unsigned MaxWidth = LastNeeded - FirstNeeded + 1;
@@ -1503,8 +1503,8 @@ Instruction *ConstantLoader::loadNonPackedIntConst(Instruction *InsertBefore) {
15031503
unsigned NumElements = CTy->getNumElements();
15041504
Instruction *Result = nullptr;
15051505
for (unsigned Idx = 0; Idx != NumElements;) {
1506-
unsigned Size =
1507-
std::min(PowerOf2Floor(NumElements - Idx), (uint64_t)ImmIntVec::Width);
1506+
unsigned Size = std::min(IGCLLVM::bit_floor(NumElements - Idx),
1507+
(uint64_t)ImmIntVec::Width);
15081508
Constant *SubC = getConstantSubvector(C, Idx, Size);
15091509
Value *SubV = SubC;
15101510
ConstantLoader SubLoader(SubC, Subtarget, DL);

IGC/VectorCompiler/lib/GenXCodeGen/GenXLegalization.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,8 +1217,7 @@ unsigned GenXLegalization::determineWidth(unsigned WholeWidth,
12171217
ExecSizeAllowedBits &= 0x1f;
12181218

12191219
IGC_ASSERT_EXIT(ExecSizeAllowedBits > 0);
1220-
unsigned MainInstMinWidth = 1
1221-
<< llvm::countTrailingZeros(ExecSizeAllowedBits);
1220+
unsigned MainInstMinWidth = 1 << IGCLLVM::countr_zero(ExecSizeAllowedBits);
12221221
// Determine the vector width that we need to split into.
12231222
bool IsReadSameVector = false;
12241223
unsigned Width = WholeWidth - StartIdx;

IGC/VectorCompiler/lib/GenXCodeGen/GenXLowerAggrCopies.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ SPDX-License-Identifier: MIT
3535
#include "llvmWrapper/IR/DerivedTypes.h"
3636
#include "llvmWrapper/IR/Type.h"
3737
#include "llvmWrapper/Support/Alignment.h"
38+
#include "llvmWrapper/Support/MathExtras.h"
3839

3940
#include "vc/GenXCodeGen/GenXLowerAggrCopies.h"
4041

@@ -98,7 +99,7 @@ struct SliceInfo {
9899
static std::vector<SliceInfo> getLegalLengths(int TotalLength, int Align) {
99100
std::vector<SliceInfo> Slices;
100101
for (int Offset = 0; TotalLength;) {
101-
int Width = PowerOf2Floor(TotalLength);
102+
int Width = IGCLLVM::bit_floor(TotalLength);
102103
Slices.push_back({Offset, Width, Align});
103104

104105
Offset += Width;

IGC/VectorCompiler/lib/GenXCodeGen/GenXLowering.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ SPDX-License-Identifier: MIT
119119
#include "llvmWrapper/IR/DerivedTypes.h"
120120
#include "llvmWrapper/IR/Instructions.h"
121121
#include "llvmWrapper/Support/TypeSize.h"
122+
#include "llvmWrapper/Support/MathExtras.h"
122123

123124
#include "vc/Support/GenXDiagnostic.h"
124125
#include "vc/Utils/GenX/GlobalVariable.h"
@@ -4206,8 +4207,9 @@ static LoHiRes buildIMadWithMadw(ArrayRef<Value *> Args, bool Signed,
42064207
LoHiRes Res = {UndefValue::get(OpTy), UndefValue::get(OpTy)};
42074208
unsigned StartIdx = 0;
42084209
while (StartIdx < OpWidth) {
4209-
unsigned SplitWidth = std::min(
4210-
TargetWidth, static_cast<unsigned>(PowerOf2Floor(OpWidth - StartIdx)));
4210+
unsigned SplitWidth =
4211+
std::min(TargetWidth,
4212+
static_cast<unsigned>(IGCLLVM::bit_floor(OpWidth - StartIdx)));
42114213

42124214
std::array<Value *, 3> SplitArgs;
42134215
for (unsigned i = 0; i < Args.size(); ++i)
@@ -5150,7 +5152,7 @@ bool GenXLowering::lowerReduction(CallInst *CI, Value *Src, Value *Start,
51505152
}
51515153
SrcWidth = LinearGrain;
51525154
} else if (!isPowerOf2_32(SrcWidth)) {
5153-
TailIndex = PowerOf2Floor(SrcWidth);
5155+
TailIndex = IGCLLVM::bit_floor(SrcWidth);
51545156
IGC_ASSERT_EXIT(TailIndex);
51555157
TailWidth = SrcWidth % TailIndex;
51565158
SrcWidth = TailIndex;

IGC/VectorCompiler/lib/GenXCodeGen/GenXLscAddrCalcFolding.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ SPDX-License-Identifier: MIT
1313
#include "vc/Utils/GenX/IntrinsicsWrapper.h"
1414

1515
#include "llvmWrapper/IR/Instructions.h"
16+
#include "llvmWrapper/Support/MathExtras.h"
1617

1718
#include "llvm/CodeGen/TargetPassConfig.h"
1819
#include "llvm/IR/InstVisitor.h"

0 commit comments

Comments
 (0)