Getting TargetLowering in AsmPrinter / Lowering constant addrspacecast

Hi,

I'd like to add support for addrspacecast in initializers of global variables, at least for the trivial case. The trivial case is if TargetLowering::isNoopAddrSpaceCast(SrcAS, DestAS) returns true. In this case the MCExpr for the addrspacecast is the MCExpr of its first operand.

It seems hard to obtain an instance of TargetLowering in AsmPrinter::lowerConstant(). Other methods in this class obtain it by calling `MF->getSubtarget().getTargetLowering()`. However, because MF is nullptr when lowering initializers of global variables, it won't work. My impression is that this is not possible because every function has its own TargetLowering instance. Is that correct?

NVPTX solves the problem by having a method on it's NVPTXAsmPrinter class, which is mostly a copy of AsmPrinter::lowerConstant() and knows how to handle addrspacecasts. However, this probably isn't a solution for e.g. X86.

Suggestions are highly welcome.

-Manuel

Yeah, NVPTX is funny and requires some extra handling; especially in regards to how such casts are emitted in the assembly. It’s certainly not ideal. We actually wrap such casts in a custom MCExpr sub-class.

Hi,

I'd like to add support for addrspacecast in initializers of global
variables, at least for the trivial case. The trivial case is if
TargetLowering::isNoopAddrSpaceCast(SrcAS, DestAS) returns true. In
this case the MCExpr for the addrspacecast is the MCExpr of its first
operand.

You could create a Util directory and put the implementation of
isNoopAddrSpaceCast in there. We do this in AMDGPU when we want
to share code between the MC and non-MC parts of the compiler.

-Tom

Hi,

I'd like to add support for addrspacecast in initializers of global
variables, at least for the trivial case. The trivial case is if
TargetLowering::isNoopAddrSpaceCast(SrcAS, DestAS) returns true. In
this case the MCExpr for the addrspacecast is the MCExpr of its first
operand.

You could create a Util directory and put the implementation of
isNoopAddrSpaceCast in there. We do this in AMDGPU when we want
to share code between the MC and non-MC parts of the compiler.

Your suggestion is very helpful to avoid code duplication for a single target. However, it still requires every target to implement an additional target hook, instead of using the existing one.

-Manuel