Skip to content

Commit d52c3f9

Browse files
dmitryryinteligcbot
authored andcommitted
Support ptrtoint constexpr in GV initializers
Support ptrtoint constant expressions in global variable initializers.
1 parent 9c6d7c1 commit d52c3f9

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

IGC/VectorCompiler/lib/GenXCodeGen/ConstantEncoder.cpp

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ SPDX-License-Identifier: MIT
88

99
#include "ConstantEncoder.h"
1010

11+
#include "vc/Support/GenXDiagnostic.h"
12+
#include "vc/Utils/GenX/TypeSize.h"
13+
1114
#include "Probe/Assertion.h"
1215
#include "llvmWrapper/IR/DerivedTypes.h"
1316

@@ -26,11 +29,42 @@ encodeConstExprImpl(const llvm::GEPOperator &GEP, const DataLayout &DL) {
2629
return {Data + Offset, Relocs};
2730
}
2831

32+
static std::pair<APInt, std::vector<vISA::ZERelocEntry>>
33+
encodeConstExprImpl(const PtrToIntOperator &P2I, const DataLayout &DL) {
34+
IGC_ASSERT_MESSAGE(!isa<IGCLLVM::FixedVectorType>(P2I.getType()),
35+
"vector ptrtoint is not yet supported");
36+
auto [Data, Relocs] = vc::encodeGlobalValueOrConstantExpression(
37+
*cast<Constant>(P2I.getPointerOperand()), DL);
38+
auto DstSize = vc::getTypeSize(P2I.getType(), &DL);
39+
vc::TypeSizeWrapper SrcSize{Data.getBitWidth()};
40+
if (DstSize == SrcSize)
41+
// Nop ptrtoint just return the pointer operand encoding.
42+
return {Data, Relocs};
43+
44+
if (DstSize > SrcSize)
45+
// zext ptrtoint case, just append zeros to data.
46+
return {Data.zext(DstSize.inBits()), Relocs};
47+
48+
// The only supported case of truncation.
49+
if (DstSize.inBits() == 32 && SrcSize.inBits() == 64) {
50+
IGC_ASSERT_MESSAGE(
51+
Relocs.size() == 1 &&
52+
Relocs.front().r_type == vISA::GenRelocType::R_SYM_ADDR,
53+
"only 64-bit relocation is supported for a truncating ptrtoint");
54+
Relocs.front().r_type = vISA::GenRelocType::R_SYM_ADDR_32;
55+
return {Data.trunc(32), Relocs};
56+
}
57+
58+
vc::diagnose(P2I.getContext(), "ConstantEncoder", &P2I,
59+
"such ptrtoint constant expression is not supported");
60+
return {Data, Relocs};
61+
}
62+
2963
static std::pair<APInt, std::vector<vISA::ZERelocEntry>>
3064
encodeGlobalValue(const GlobalValue &GV, const DataLayout &DL) {
3165
auto RelType = vISA::GenRelocType::R_NONE;
32-
unsigned Size = DL.getTypeSizeInBits(GV.getType());
33-
switch (Size) {
66+
auto Size = vc::getTypeSize(GV.getType(), &DL);
67+
switch (Size.inBits()) {
3468
case 32:
3569
RelType = vISA::GenRelocType::R_SYM_ADDR_32;
3670
break;
@@ -40,15 +74,17 @@ encodeGlobalValue(const GlobalValue &GV, const DataLayout &DL) {
4074
default:
4175
report_fatal_error("Relocation of the provided pointer is not supported");
4276
}
43-
return {APInt::getNullValue(Size),
77+
return {APInt::getNullValue(Size.inBits()),
4478
{vISA::ZERelocEntry{RelType, 0, GV.getName().str()}}};
4579
}
4680

4781
static std::pair<APInt, std::vector<vISA::ZERelocEntry>>
4882
encodeConstExpr(const ConstantExpr &CExpr, const DataLayout &DL) {
4983
switch (CExpr.getOpcode()) {
50-
case llvm::Instruction::GetElementPtr:
51-
return encodeConstExprImpl(llvm::cast<llvm::GEPOperator>(CExpr), DL);
84+
case Instruction::GetElementPtr:
85+
return encodeConstExprImpl(cast<GEPOperator>(CExpr), DL);
86+
case Instruction::PtrToInt:
87+
return encodeConstExprImpl(cast<PtrToIntOperator>(CExpr), DL);
5288
default:
5389
IGC_ASSERT_MESSAGE(0, "Unsupported constant expression");
5490
return {APInt{}, {}};

0 commit comments

Comments
 (0)