This is an archive of the discontinued LLVM Phabricator instance.

[CodeGen] Fixed ambiguous symbol ExtAddrMode in case of NDEBUG and LLVM_ENABLE_DUMP
ClosedPublic

Authored by slydiman on Jul 23 2022, 9:33 AM.

Details

Summary

This patch fixes the following error with MSVC 16.9.2 in case of NDEBUG and LLVM_ENABLE_DUMP:
llvm/lib/CodeGen/CodeGenPrepare.cpp(2581): error C2872: 'ExtAddrMode': ambiguous symbol
llvm/include/llvm/CodeGen/TargetInstrInfo.h(86): note: could be 'llvm::ExtAddrMode'
llvm/lib/CodeGen/CodeGenPrepare.cpp(2447): note: or '`anonymous-namespace'::ExtAddrMode'
llvm/lib/CodeGen/CodeGenPrepare.cpp(2581): error C2039: 'print': is not a member of 'llvm::ExtAddrMode'

Diff Detail

Event Timeline

slydiman created this revision.Jul 23 2022, 9:33 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 23 2022, 9:33 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
slydiman requested review of this revision.Jul 23 2022, 9:33 AM
barannikov88 added inline comments.
llvm/lib/CodeGen/CodeGenPrepare.cpp
2618

Please refer to the coding style, in particular:

Because of this, we have a simple guideline: make anonymous namespaces as small as possible, and only use them for class declarations.

slydiman added inline comments.Jul 23 2022, 10:51 AM
llvm/lib/CodeGen/CodeGenPrepare.cpp
2618

It seems the function void ExtAddrMode::print() is related to the struct ExtAddrMode declared above inside the anonymous namespace.
Please correct me if I'm wrong. Do you have any suggestions how to fix this error other way?

slydiman retitled this revision from [CodeGen] Fixed ambiguous symbol `ExtAddrMode` in case of NDEBUG to [CodeGen] Fixed ambiguous symbol `ExtAddrMode` in case of LLVM_ENABLE_DUMP.Jul 23 2022, 11:02 AM
slydiman edited the summary of this revision. (Show Details)
slydiman updated this revision to Diff 447092.Jul 23 2022, 11:09 AM
slydiman retitled this revision from [CodeGen] Fixed ambiguous symbol `ExtAddrMode` in case of LLVM_ENABLE_DUMP to [CodeGen] Fixed ambiguous symbol `ExtAddrMode` in case of NDEBUG and LLVM_ENABLE_DUMP.
slydiman edited the summary of this revision. (Show Details)
slydiman added a reviewer: aaron.ballman.
barannikov88 added inline comments.Jul 23 2022, 11:32 AM
llvm/lib/CodeGen/CodeGenPrepare.cpp
2618

print is defined under #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP).
It should be declared under the same macros. Same for dump.

As for the error, it seems that it is picking ExtAddrMode from TargetInstrInfo.h somehow (note that it says 'print': is not a member of 'llvm::ExtAddrMode',) though I could not find where this header is included. Are you sure you don't have any local changes that may trigger the issue?

barannikov88 added inline comments.Jul 23 2022, 11:39 AM
llvm/lib/CodeGen/CodeGenPrepare.cpp
2578

It should be left as it was. The intention is to allow printing if either of the macros is defined:
option(LLVM_ENABLE_DUMP "Enable dump functions even when assertions are disabled" OFF)

slydiman updated this revision to Diff 447095.Jul 23 2022, 11:49 AM
slydiman retitled this revision from [CodeGen] Fixed ambiguous symbol `ExtAddrMode` in case of NDEBUG and LLVM_ENABLE_DUMP to [CodeGen] Fixed ambiguous symbol ExtAddrMode in case of NDEBUG and LLVM_ENABLE_DUMP.
slydiman added inline comments.Jul 23 2022, 11:53 AM
llvm/lib/CodeGen/CodeGenPrepare.cpp
2618

I have no any changes. Just defined NDEBUG and LLVM_ENABLE_DUMP.

barannikov88 added inline comments.Jul 23 2022, 12:06 PM
llvm/lib/CodeGen/CodeGenPrepare.cpp
2618

I have no any changes.

According to the description, the compiler complains about line 2562, which does not contain 'printf', so I assumed that you have modified the source (or, at least, have an outdated version).

Just defined NDEBUG and LLVM_ENABLE_DUMP.

Defining both of these macros does not trigger the error for me. But my build OS is Linux.

I have no other ideas what could go wrong here, maybe others do?

slydiman edited the summary of this revision. (Show Details)Jul 23 2022, 12:06 PM
slydiman edited the summary of this revision. (Show Details)Jul 23 2022, 12:09 PM
slydiman added inline comments.
llvm/lib/CodeGen/CodeGenPrepare.cpp
2618

I have updated the summary with more details.

slydiman edited the summary of this revision. (Show Details)Jul 23 2022, 12:14 PM
barannikov88 added inline comments.Jul 23 2022, 12:17 PM
llvm/lib/CodeGen/CodeGenPrepare.cpp
2618

So, it is indeed confused with the same-named class from TargetInstrInfo.h. Could you track where it is included?
Adding #error directive to TargetInstrInfo.h and recompiling single CodeGenPrepare.cpp should help to identify this place. For me, however, adding this directive does not prevent the cpp file from being successfully compiled.

barannikov88 added inline comments.Jul 23 2022, 12:26 PM
llvm/lib/CodeGen/CodeGenPrepare.cpp
2618

Calling ::print (with double colon) from the problematic place might workaround the issue.

barannikov88 added inline comments.Jul 23 2022, 12:36 PM
llvm/lib/CodeGen/CodeGenPrepare.cpp
2618

Never mind :)

aaron.ballman added inline comments.Jul 25 2022, 8:37 AM
llvm/lib/CodeGen/CodeGenPrepare.cpp
2618

+1 to not extending the anonymous namespace like this but instead using a qualified identifier as necessary.

slydiman added inline comments.Jul 25 2022, 3:12 PM
llvm/lib/CodeGen/CodeGenPrepare.cpp
2618

You do realize that one cannot provide qualified name for unnamed namespace, right? But please feel free to explain if I’m missing something in your comment.
I don’t see how this change possibly violates the developers policy, since this is exactly what is done here - a class declared in an unnamed namespace along with all its memebrs to fix the defect of possible referencing symbols from outside of this CU.
As another approach, we could make that a named namespace, and use qualified names instead. This would also change the symbol visibility/linkage, which I personally do not like.

barannikov88 added inline comments.Jul 25 2022, 4:10 PM
llvm/lib/CodeGen/CodeGenPrepare.cpp
2618

@slydiman
Have you been able to find the place where TargetInstrInfo.h is included? Or is it some MSVC quirk that looks arcoss CUs?

slydiman added inline comments.Jul 25 2022, 4:37 PM
llvm/lib/CodeGen/CodeGenPrepare.cpp
2618

Unfortunately, I did not find the exact place.
I can reproduce the problem on one of my setups.
It looks like a MSVC quirk around precompiled headers.
As another approach, we could just rename ExtAddrMode to something like ExtTargetLoweringAddrMode keeping the anonymous namespace untouched.

barannikov88 added inline comments.Jul 25 2022, 4:55 PM
llvm/lib/CodeGen/CodeGenPrepare.cpp
2618

Renaming TII's sounds reasonable to me. Something as general as "ExtAddrMode" should not have been added to llvm namespace in the first place (it is not that fundamental class to do so).

Another option is to try to move the class into TargetInstrInfo class declaration.

Either of these option will require changing backends.

If the intention is to just to make it work, renaming the local class seems to be much easier task. Or, alternatively, you could introduce another namespace under the anonymous one. This will keep the class hidden, while disambiguating it with TII's class. You don't see this very often though.

aaron.ballman accepted this revision.Jul 26 2022, 7:28 AM

LGTM!

llvm/lib/CodeGen/CodeGenPrepare.cpp
2618

You do realize that one cannot provide qualified name for unnamed namespace, right? But please feel free to explain if I’m missing something in your comment.

I was thinking we'd be dropping the using namespace llvm; but I realize now, that's going to be fragile and not really fix the underlying issue. Sorry for the noise there!

I'm actually fine with these changes, the alternatives require more invasive changes that I don't think are worth it.

This revision is now accepted and ready to land.Jul 26 2022, 7:28 AM
barannikov88 added inline comments.Jul 26 2022, 7:49 AM
llvm/lib/CodeGen/CodeGenPrepare.cpp
2618

I'm actually fine with these changes, the alternatives require more invasive changes that I don't think are worth it.

From that point of view, looks good to me, too.

This revision was landed with ongoing or failed builds.Jul 26 2022, 3:22 PM
This revision was automatically updated to reflect the committed changes.