This is an archive of the discontinued LLVM Phabricator instance.

[PowerPC] Add .ref in backend for AIX XCOFF to support `-bcdtors:csect` linker option
Needs ReviewPublic

Authored by tingwang on Mar 21 2022, 6:12 PM.


Group Reviewers
Restricted Project

On AIX -bcdtors:csect linker option requires ".ref" pseudo-op to indicate the connection among static variables (static global variable, static class member etc.) and static init/term functions, so that linker can generate the correct output.

Here in IR we borrow ‘associated’ metadata to indicate the relationship for ".ref". There are three types of association need be addressed:
(1) variable to init/term functions (required to add functions to _cdtors array if the variable is included in linker output)
(2) dtor function to term function (required to correctly handle atexit/unatexit registration/unregistration)
(3) program code csect to internal variable classified as Data or ThreadData

This is the first half change to support -bcdtors:csect linker option, it adds ".ref" in case (3), and emit ".ref" in assembly output. The other two items will be handled in clang.

Diff Detail

Event Timeline

tingwang created this revision.Mar 21 2022, 6:12 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 21 2022, 6:12 PM
tingwang requested review of this revision.Mar 21 2022, 6:12 PM
shchenz added inline comments.Apr 6 2022, 7:22 AM
625 ↗(On Diff #417140)

Can we only use one parameter const MCSymbol *Symbol for this function? And update other callers accordingly. These two parameters are kind of duplicate.

2335 ↗(On Diff #417140)

However .ref does not support BS mapping class.

This seems weird. Should be "csect with BS storage mapping class can not contain .ref"?


can we change the GVkind here for static globals with associated metadata? We use BSS kind for normal static globals, maybe we can change it to RW, so that we may don't need to change MCSectionXCOFF::printSwitchToSection and TargetLoweringObjectFileXCOFF::SelectSectionForGlobal?


nit: !GV->getMetadata(LLVMContext::MD_associated) seems more common

shchenz added inline comments.Apr 6 2022, 7:25 AM

Maybe we can add an assert here that we are not adding .ref in a section with BS/UC storage mapping class.

tingwang updated this revision to Diff 421158.Apr 7 2022, 4:45 AM
tingwang marked 4 inline comments as done.

Update based on Chen Zheng's comments:
(1) Corrected emitXCOFFRefDirective argument, the original one should be fine.
(2) Add assert check to guard against BS and UC mapping class.
(3) Updated code comment and code style.


My initial thought was the same. After looking into TargetLoweringObjectFile::getKindForGlobal(), I didn't find any point for platform specific logic to change the decision, and thus I turned into our platform dependent code to make changes here. (If there is already one hook, I will be happy to use that. However at this moment, I don't see any for this purpose.)

shchenz added inline comments.Apr 7 2022, 7:00 PM

can we use getCurrentSectionOnly()?


Is it possible to custom the GVKind in current function? This function is AIX specific?

tingwang marked an inline comment as done.Apr 8 2022, 8:09 AM
tingwang added inline comments.

If I'm understanding correct, this is customize in the middle (my original approach is customize in the end). The trace log shows that getKindForGlobal gets called from different places, for example: GlobalMerge.cpp:::GlobalMerge::doInitialization,
PPCAsmPrinter.cpp:::PPCAIXAsmPrinter::doInitialization, TargetLoweringObjectFile.cpp:::TargetLoweringObjectFile::SectionForGlobal, TargetLoweringObjectFileImpl.cpp:::TargetLoweringObjectFileXCOFF::getTargetSymbol. Customizing in the middle means we have to cover all above code path for correct behavior, and we also have to take care of any more upcoming code changes regarding this API usage. Probably the original approach is simpler.

tingwang updated this revision to Diff 421527.Apr 8 2022, 8:11 AM

Add assert to guard MCSection

During test some case revealed another issue regarding BSSLocal: there maybe some function associated with variable which has storage class BSSLocal (zero initialized). Unfortunately the variable is the association target, and there is no associated metadata attached to the variable. I have to work out a solution for this, incorporating Zheng's comments here. I will update code once the solution is formed.

tingwang updated this revision to Diff 427503.May 5 2022, 6:26 PM
tingwang edited the summary of this revision. (Show Details)
tingwang updated this revision to Diff 428259.May 9 2022, 7:02 PM

Use opaque-pointer in testcase aix-xcoff-ref.ll

jsji resigned from this revision.Jun 2 2022, 7:52 AM
tingwang updated this revision to Diff 443116.Jul 7 2022, 7:40 PM

Rebase && drop TLS related .ref for now

I'm not familiar with a lot of this code, but I don't see any obvious errors.

I'm not familiar with a lot of this code, but I don't see any obvious errors.

Thank you for help!