This is an archive of the discontinued LLVM Phabricator instance.

[XCOFF][AIX] Add Global Variables Directly to TOC for 32 bit AIX
ClosedPublic

Authored by sidbav on Apr 23 2021, 9:23 AM.

Details

Summary

This patch implements the backend implementation of adding global variables directly to the table of contents, rather than adding the address of the variable to the table of contents.

Currently, this patch will look for the "toc-data" attribute on symbols in LLVM IR, and then add those symbols to the toc. This patch assumes that the symbols which have the toc-data attribute can be added to the TOC with no issues (strictly aligned, pointer sized).

ATM, this is implemented for 32 bit AIX.

Diff Detail

Event Timeline

sidbav created this revision.Apr 23 2021, 9:23 AM
sidbav requested review of this revision.Apr 23 2021, 9:23 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 23 2021, 9:23 AM
sfertile added inline comments.Apr 26 2021, 9:55 AM
llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp
2156

I think we need to explain why we return early here. Maybe change , return
to
'it needs to be emitted later, when we emit the .toc section.'

2273–2274

Need to update this comment. if there are no functions in the module, and no toc-data definitions

2309

Please address the clang-tidy warning.

llvm/lib/Target/PowerPC/PPCISelDAGToDAG.cpp
5603

Since all we need is true/false for if the global has the toc-data attribute I think we should factor all this casting into an appropriately named helper function. Then we can change the replaceWithLWZtoc lambda to take an opcode argument and reuse it for transforming the node.

A helper along the lines of

bool hasTocDataAttr(SDValue Val) {
   GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Val);
   if (!GA)
     return false;

   const GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(GA->getGlobal());
  if (!GV)
    return false;

  if (!GV->hasAttribute("toc-data"))
    return false;


  // TODO assert on alignment and size ...?

  return true;
}
5607–5609

Rename this and add a new parameter auto replaceWith = [this, &dl](unsigned Opcode, SDNode *TocEntry).
Then change the hardcoded LWZtoc to the opcode argument, and move where we do the toc-data transformation down to existing if (isAIXABI && CModel == CodeModel::Small) block on line 5584.

llvm/test/CodeGen/PowerPC/basic-toc-data-2.ll
12 ↗(On Diff #340064)

There is trailing whitespace on this line.

sfertile added inline comments.Apr 26 2021, 10:17 AM
llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
2207

No need to convert from GlobalObject --> GlobalValue --> GlobalVariable. Just try to dyn_cast from GlobalObject -> GlobalVariable

2252

Same: no need to convert the GlobalObject to a GlobalValue before tying to cast to a GlobalVariable.

sidbav updated this revision to Diff 340691.Apr 26 2021, 5:04 PM

Addressing Review Comments

sidbav marked 8 inline comments as done.Apr 26 2021, 5:05 PM

Thanks for the quick updates. Patch is getting really close I think.

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
2206

Real minor nit: missing the period at the end of the comment.

2250

ditto.

llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp
866

Missing the period.

869

Real minor nit: Probably useful to use 'load address' instead of 'la', and missing the period at the end of the sentance.

llvm/lib/Target/PowerPC/PPCISelDAGToDAG.cpp
449

I'm not sure of the limitations of the option in XL that enables toc-data. For example if it can it be used to put a vector with an alignment/size of 16 in the TOC, or structures, arrays, etc; For now we don't have any testing coverage for these things so I think we should add some defensive errors here.

  • If the alignment is greater then the alignment of a toc-entry it should be an error.
  • If the size of the type is larger then a toc-entry it should be an error.
  • For now, if the GlobalVariable is an array type or aggregate type we should error.
jsji added a reviewer: Restricted Project.Apr 26 2021, 7:14 PM
sfertile added inline comments.Apr 27 2021, 5:50 AM
llvm/test/CodeGen/PowerPC/toc-data.ll
4

Add a run step which compiles to assembly, and the associated checks.

sidbav updated this revision to Diff 340958.Apr 27 2021, 12:59 PM

Addressing Sean's comments

sidbav marked 6 inline comments as done.Apr 27 2021, 1:01 PM
sfertile added inline comments.Apr 28 2021, 12:23 PM
llvm/lib/Target/PowerPC/PPCISelDAGToDAG.cpp
468

Minor nit: since most of these limitations are because this feature is a work in progress, we should change the asserts to report_fatal_error. The assert that the type is sized can stay an assert because we expect that to stay a limitation, and because I would expect the user facing option implementation to have already checked that and emitted some kind of error from the front-end.

484

I would split out 'hasCommonLinkage() into its own assert with the string saying something along the lines of tentative definitions cannot have mapping class XMC_TD`.

The local linkage limitation I believe we hit when compiling spec right? In that case maybe change the message to 'local linkage globals cannot have mapping class XMC_TD`. If we didn't, then make sure to test with the system assembler first to make sure it is invalid to define a local linkage global in the .toc.

llvm/test/CodeGen/PowerPC/basic-toc-data-2.ll
1 ↗(On Diff #340958)

Minor nit: name this file basic-toc-data-extern.ll

2 ↗(On Diff #340958)

Since this patch is only going to add assembly support for this feature, we need to also compile this test to an object file and make sure we fail gracefully. Add a run-step:

; RUN: not --crash llc -filetype=obj -mtriple powerpc-ibm-aix-xcoff  \
; RUN:                 -verify-machineinstrs < %s 2>&1 | \
; RUN:   FileCheck %s --check-prefix=OBJ

with a check: ; OBJ: LLVM ERROR: toc-data not yet supported when writing object files.

And in llvm/lib/MC/XCOFFObjectWriter.cpp add

@@ -439,6 +441,10 @@ void XCOFFObjectWriter::recordRelocation(MCAssembler &Asm,
       TargetObjectWriter->getRelocTypeAndSignSize(Target, Fixup, IsPCRel);
 
   const MCSectionXCOFF *SymASec = getContainingCsect(cast<MCSymbolXCOFF>(SymA));
+
+  if (SymASec->isCsect() && SymASec->getMappingClass() == XCOFF::XMC_TD)
+    report_fatal_error("toc-data not yet supported when writing object files.");
+
   assert(SectionMap.find(SymASec) != SectionMap.end() &&
          "Expected containing csect to exist in map.");

to emit the error.

llvm/test/CodeGen/PowerPC/basic-toc-data.ll
1 ↗(On Diff #340958)

minor nit: name this file basic-toc-data-def.ll

2 ↗(On Diff #340958)

We need to similarly fail gracefully for this test when editing an object file, and add test coverage that we do so.

Run-step:

; RUN: not --crash llc -filetype=obj -mtriple powerpc-ibm-aix-xcoff  \
; RUN:                 -verify-machineinstrs < %s 2>&1 | \
; RUN:   FileCheck %s --check-prefix=OBJ

and to get the error emitted, add a switch case for XMC_TD: in XCOFFObjectWriter::getCsectGroup which emits the error.

llvm/test/CodeGen/PowerPC/toc-data.ll
22

minor nit: indent the instructions 2 spaces past the label, ditto for the other checks below.

sfertile added inline comments.Apr 28 2021, 6:13 PM
llvm/lib/Target/PowerPC/PPCISelDAGToDAG.cpp
484

I did some poking, and the problem is that clang use .lcomm to emit local linkage for internal/private linkage globals. I believe we will be able to support local linkage toc-data by skipping the lcomm directive like

.csect i[TD],2
 .align 2
 .vbyte 4, <val>

So make the local/private linkage a fatal error instead of an assert (since its going to be a temporary limitation)

llvm/test/CodeGen/PowerPC/toc-data.ll
74

minor nit: name should be read_i32_local_linkage()

Also please move this into its own file, and put the toc-data attribute on ilocal and check for an error message.

sidbav updated this revision to Diff 341605.Apr 29 2021, 12:15 PM

replacing some of the asserts with report_fatal_error, renaming some of the lit tests, and create a new one for local linkage

sidbav marked 8 inline comments as done.Apr 29 2021, 12:16 PM
sfertile accepted this revision as: sfertile.Apr 30 2021, 6:13 AM

Great work Sidharth, a couple minor nits but otherwise the patch LGTM.

llvm/lib/Target/PowerPC/PPCISelDAGToDAG.cpp
456

Minor nit: reword to "GlobalVariables with an alignment requirement stricter then 4-bytes not supported by the toc data transformation"

465

minor nit: 'size must be know' -> 'size must be known'.

489

minor nit: pare down to just "Tentative definitions cannot have mapping class XMC_TD.".

This revision is now accepted and ready to land.Apr 30 2021, 6:13 AM
This revision was landed with ongoing or failed builds.Apr 30 2021, 7:49 AM
This revision was automatically updated to reflect the committed changes.
sidbav marked 3 inline comments as done.Apr 30 2021, 8:03 AM

Addressed the all of the remaining comments in the commit.