Skip to content

Commit 3d1705d

Browse files
authored
MCExpr-ify AMDGPU PALMetadata (#93236)
Allows MCExprs as passed values to PALMetadata. Also adds related `DelayedMCExpr` classes which serve as a pseudo-fixup to resolve MCExprs as late as possible (i.e., right before emit through string or blob, where they should be resolvable).
1 parent ffab938 commit 3d1705d

15 files changed

+601
-61
lines changed

llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "TargetInfo/AMDGPUTargetInfo.h"
3030
#include "Utils/AMDGPUBaseInfo.h"
3131
#include "Utils/AMDKernelCodeTUtils.h"
32+
#include "Utils/SIDefinesUtils.h"
3233
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
3334
#include "llvm/BinaryFormat/ELF.h"
3435
#include "llvm/CodeGen/MachineFrameInfo.h"
@@ -1234,41 +1235,49 @@ void AMDGPUAsmPrinter::EmitPALMetadata(const MachineFunction &MF,
12341235
auto &Ctx = MF.getContext();
12351236

12361237
MD->setEntryPoint(CC, MF.getFunction().getName());
1237-
MD->setNumUsedVgprs(
1238-
CC, getMCExprValue(CurrentProgramInfo.NumVGPRsForWavesPerEU, Ctx));
1238+
MD->setNumUsedVgprs(CC, CurrentProgramInfo.NumVGPRsForWavesPerEU, Ctx);
12391239

12401240
// Only set AGPRs for supported devices
12411241
const GCNSubtarget &STM = MF.getSubtarget<GCNSubtarget>();
12421242
if (STM.hasMAIInsts()) {
1243-
MD->setNumUsedAgprs(CC, getMCExprValue(CurrentProgramInfo.NumAccVGPR, Ctx));
1243+
MD->setNumUsedAgprs(CC, CurrentProgramInfo.NumAccVGPR);
12441244
}
12451245

1246-
MD->setNumUsedSgprs(
1247-
CC, getMCExprValue(CurrentProgramInfo.NumSGPRsForWavesPerEU, Ctx));
1246+
MD->setNumUsedSgprs(CC, CurrentProgramInfo.NumSGPRsForWavesPerEU, Ctx);
12481247
if (MD->getPALMajorVersion() < 3) {
1249-
MD->setRsrc1(CC, CurrentProgramInfo.getPGMRSrc1(CC, STM));
1248+
MD->setRsrc1(CC, CurrentProgramInfo.getPGMRSrc1(CC, STM, Ctx), Ctx);
12501249
if (AMDGPU::isCompute(CC)) {
1251-
MD->setRsrc2(CC, CurrentProgramInfo.getComputePGMRSrc2());
1250+
MD->setRsrc2(CC, CurrentProgramInfo.getComputePGMRSrc2(Ctx), Ctx);
12521251
} else {
1253-
if (getMCExprValue(CurrentProgramInfo.ScratchBlocks, Ctx) > 0)
1254-
MD->setRsrc2(CC, S_00B84C_SCRATCH_EN(1));
1252+
const MCExpr *HasScratchBlocks =
1253+
MCBinaryExpr::createGT(CurrentProgramInfo.ScratchBlocks,
1254+
MCConstantExpr::create(0, Ctx), Ctx);
1255+
auto [Shift, Mask] = getShiftMask(C_00B84C_SCRATCH_EN);
1256+
MD->setRsrc2(CC, maskShiftSet(HasScratchBlocks, Mask, Shift, Ctx), Ctx);
12551257
}
12561258
} else {
12571259
MD->setHwStage(CC, ".debug_mode", (bool)CurrentProgramInfo.DebugMode);
1258-
MD->setHwStage(CC, ".scratch_en",
1259-
(bool)getMCExprValue(CurrentProgramInfo.ScratchEnable, Ctx));
1260+
MD->setHwStage(CC, ".scratch_en", msgpack::Type::Boolean,
1261+
CurrentProgramInfo.ScratchEnable);
12601262
EmitPALMetadataCommon(MD, CurrentProgramInfo, CC, STM);
12611263
}
12621264

12631265
// ScratchSize is in bytes, 16 aligned.
12641266
MD->setScratchSize(
1265-
CC, alignTo(getMCExprValue(CurrentProgramInfo.ScratchSize, Ctx), 16));
1267+
CC,
1268+
AMDGPUVariadicMCExpr::createAlignTo(CurrentProgramInfo.ScratchSize,
1269+
MCConstantExpr::create(16, Ctx), Ctx),
1270+
Ctx);
1271+
12661272
if (MF.getFunction().getCallingConv() == CallingConv::AMDGPU_PS) {
12671273
unsigned ExtraLDSSize = STM.getGeneration() >= AMDGPUSubtarget::GFX11
12681274
? divideCeil(CurrentProgramInfo.LDSBlocks, 2)
12691275
: CurrentProgramInfo.LDSBlocks;
12701276
if (MD->getPALMajorVersion() < 3) {
1271-
MD->setRsrc2(CC, S_00B02C_EXTRA_LDS_SIZE(ExtraLDSSize));
1277+
MD->setRsrc2(
1278+
CC,
1279+
MCConstantExpr::create(S_00B02C_EXTRA_LDS_SIZE(ExtraLDSSize), Ctx),
1280+
Ctx);
12721281
MD->setSpiPsInputEna(MFI->getPSInputEnable());
12731282
MD->setSpiPsInputAddr(MFI->getPSInputAddr());
12741283
} else {
@@ -1315,20 +1324,19 @@ void AMDGPUAsmPrinter::emitPALFunctionMetadata(const MachineFunction &MF) {
13151324

13161325
if (MD->getPALMajorVersion() < 3) {
13171326
// Set compute registers
1318-
MD->setRsrc1(CallingConv::AMDGPU_CS,
1319-
CurrentProgramInfo.getPGMRSrc1(CallingConv::AMDGPU_CS, ST));
1327+
MD->setRsrc1(
1328+
CallingConv::AMDGPU_CS,
1329+
CurrentProgramInfo.getPGMRSrc1(CallingConv::AMDGPU_CS, ST, Ctx), Ctx);
13201330
MD->setRsrc2(CallingConv::AMDGPU_CS,
1321-
CurrentProgramInfo.getComputePGMRSrc2());
1331+
CurrentProgramInfo.getComputePGMRSrc2(Ctx), Ctx);
13221332
} else {
13231333
EmitPALMetadataCommon(MD, CurrentProgramInfo, CallingConv::AMDGPU_CS, ST);
13241334
}
13251335

13261336
// Set optional info
13271337
MD->setFunctionLdsSize(FnName, CurrentProgramInfo.LDSSize);
1328-
MD->setFunctionNumUsedVgprs(
1329-
FnName, getMCExprValue(CurrentProgramInfo.NumVGPRsForWavesPerEU, Ctx));
1330-
MD->setFunctionNumUsedSgprs(
1331-
FnName, getMCExprValue(CurrentProgramInfo.NumSGPRsForWavesPerEU, Ctx));
1338+
MD->setFunctionNumUsedVgprs(FnName, CurrentProgramInfo.NumVGPRsForWavesPerEU);
1339+
MD->setFunctionNumUsedSgprs(FnName, CurrentProgramInfo.NumSGPRsForWavesPerEU);
13321340
}
13331341

13341342
// This is supposed to be log2(Size)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//===- AMDGPUDelayedMCExpr.cpp - Delayed MCExpr resolve ---------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "AMDGPUDelayedMCExpr.h"
10+
#include "llvm/MC/MCExpr.h"
11+
#include "llvm/MC/MCValue.h"
12+
13+
using namespace llvm;
14+
15+
static msgpack::DocNode getNode(msgpack::DocNode DN, msgpack::Type Type,
16+
MCValue Val) {
17+
msgpack::Document *Doc = DN.getDocument();
18+
switch (Type) {
19+
default:
20+
return Doc->getEmptyNode();
21+
case msgpack::Type::Int:
22+
return Doc->getNode(static_cast<int64_t>(Val.getConstant()));
23+
case msgpack::Type::UInt:
24+
return Doc->getNode(static_cast<uint64_t>(Val.getConstant()));
25+
case msgpack::Type::Boolean:
26+
return Doc->getNode(static_cast<bool>(Val.getConstant()));
27+
}
28+
}
29+
30+
void DelayedMCExprs::assignDocNode(msgpack::DocNode &DN, msgpack::Type Type,
31+
const MCExpr *ExprValue) {
32+
MCValue Res;
33+
if (ExprValue->evaluateAsRelocatable(Res, nullptr, nullptr)) {
34+
if (Res.isAbsolute()) {
35+
DN = getNode(DN, Type, Res);
36+
return;
37+
}
38+
}
39+
40+
DelayedExprs.push_back(Expr{DN, Type, ExprValue});
41+
}
42+
43+
bool DelayedMCExprs::resolveDelayedExpressions() {
44+
while (!DelayedExprs.empty()) {
45+
Expr DE = DelayedExprs.front();
46+
MCValue Res;
47+
48+
if (!DE.ExprValue->evaluateAsRelocatable(Res, nullptr, nullptr) ||
49+
!Res.isAbsolute())
50+
return false;
51+
52+
DelayedExprs.pop_front();
53+
DE.DN = getNode(DE.DN, DE.Type, Res);
54+
}
55+
56+
return true;
57+
}
58+
59+
void DelayedMCExprs::clear() { DelayedExprs.clear(); }
60+
61+
bool DelayedMCExprs::empty() { return DelayedExprs.empty(); }
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===- AMDGPUDelayedMCExpr.h - Delayed MCExpr resolve -----------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPUDELAYEDMCEXPR_H
10+
#define LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPUDELAYEDMCEXPR_H
11+
12+
#include "llvm/BinaryFormat/MsgPackDocument.h"
13+
#include <deque>
14+
15+
namespace llvm {
16+
class MCExpr;
17+
18+
class DelayedMCExprs {
19+
struct Expr {
20+
msgpack::DocNode &DN;
21+
msgpack::Type Type;
22+
const MCExpr *ExprValue;
23+
Expr(msgpack::DocNode &DN, msgpack::Type Type, const MCExpr *ExprValue)
24+
: DN(DN), Type(Type), ExprValue(ExprValue) {}
25+
};
26+
27+
std::deque<Expr> DelayedExprs;
28+
29+
public:
30+
bool resolveDelayedExpressions();
31+
void assignDocNode(msgpack::DocNode &DN, msgpack::Type Type,
32+
const MCExpr *ExprValue);
33+
void clear();
34+
bool empty();
35+
};
36+
37+
} // end namespace llvm
38+
39+
#endif // LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPUDELAYEDMCEXPR_H

0 commit comments

Comments
 (0)