This is an archive of the discontinued LLVM Phabricator instance.

[llvm-objdump] Only print unrecognized CPU warning once
AbandonedPublic

Authored by DavidSpickett on Dec 1 2020, 3:28 AM.

Details

Summary

This would print the warning twice:
$ ./bin/llvm-objdump -d /tmp/test.o --mcpu=not-a-cpu
'not-a-cpu' is not a recognized processor for this target (ignoring processor)
'not-a-cpu' is not a recognized processor for this target (ignoring processor)

/tmp/test.o: file format elf64-x86-64
<...>

This is due to warnings printed from InitMCProcessorInfo.
First it calls getFeatures, which warns for an invalid CPU name.
Then it calls getSchedModelForCPU which warns again for the
same reason.

To fix this, don't print a warning in getSchedModelForCPU.

With one exception, this function is only called from
InitMCProcessorInfo so there's already been a warning printed.

The exception is ARMSubtarget::initSubtargetFeatures.
However, this function first calls ParseSubtargetFeatures,
which always calls InitMCProcessorInfo. So again we will
have already printed a warning by the time we call
getSchedModelForCPU.

This actually affects many llvm tools. llvm-objdump, llvm-mc,
llc, lli, llvm-mca and exegis to my knowledge.

However they are all calling the same code so I'm adding
tests just to llvm-objdump which will catch any regression.

--mattr doesn't have this issue and a test is added to
check this.

Diff Detail

Event Timeline

DavidSpickett created this revision.Dec 1 2020, 3:28 AM
Herald added a reviewer: MaskRay. · View Herald Transcript
Herald added a project: Restricted Project. · View Herald Transcript
DavidSpickett requested review of this revision.Dec 1 2020, 3:28 AM

Rather than putting these tests in the tools directory, it probably makes more sense to put it in the MC directory, since this is really a test that the library is sane.

Actually, more generally this warning shouldn't be printed directly to errs() in the lower-level code, but rather somehow fed back to the tool, so that it can handle it in a tool-specific way. See my lightning talk on "Error Handling in Libraries" from the 2018 developers' meeting for the reasons behind this and some suggested techniques. However, I accept that this might be a wider change than you're anticipating, so happy if you want to ignore it.

llvm/lib/MC/MCSubtargetInfo.cpp
314

I know this is the current state of things, but I am slightly hesitant that a future user might not call the getFeatures() function for some reason, so the warning will be lost. I don't have a good solution to this at this point though. In llvm-readelf, we have a reportUniqueWarning function which only prints a warning that hasn't already been reported. This doesn't exist in other tools to my knowledge however.

llvm/test/tools/llvm-objdump/warn-mattr-unrecognized.test
1

I recommend adding a top-level comment to the test explaining what this test is testing. I've found those useful, even when it seems obvious from the test name.

3

REQUIRES usually go at the start of the file in my experience.

7
12–15

Not really a big issue in this case, but I prefer to not have excessive whitespace in YAML blocks.

llvm/test/tools/llvm-objdump/warn-mcpu-unrecognized.test
1

All the comments in the other test apply to this one too.

DavidSpickett planned changes to this revision.Dec 2 2020, 4:16 AM

Actually, more generally this warning shouldn't be printed directly to errs() in the lower-level code, but rather somehow fed back to the tool

Agreed, I will try that out. Alternatively I might be able to avoid the lone getSchedModel call in the Arm target and make it harder to do things incorrectly.

DavidSpickett abandoned this revision.Feb 10 2022, 2:52 AM