This is an archive of the discontinued LLVM Phabricator instance.

Invert ArchSpec<->Platform dependency
ClosedPublic

Authored by labath on Oct 27 2017, 1:22 PM.

Details

Summary

ArchSpec::SetTriple was taking a Platform as an argument, and used it to
fill in missing pieces of the specified triple. I invert the dependency
by moving this code to other classes. For this purpose, I've created
three new functions.

  • HostInfo::GetAugmentedArchSpec: fills in the triple using the host platform (this used to be implemented by passing a null platform pointer). By putting this code in the Host module, we can provide a way to anyone who does not have a platform instance (lldb-server) an easy way to get Host data.
  • Platform::GetAugmentedArchSpec: if you have a platform instance, you can call this to let it fill in the triple.
  • static Platform::GetAugmentedArchSpec: implements the "if platform == 0 then use_host() else use_platform()" part.

After this change, it becomes possible to move the ArchSpec into the
Utility module, as it has no external dependencies.

Event Timeline

labath created this revision.Oct 27 2017, 1:22 PM
zturner added inline comments.Oct 27 2017, 2:06 PM
include/lldb/Host/HostInfoBase.h
85

s/contains/does/

include/lldb/Target/Platform.h
524

Should this function be marked const?

Another possible interface would be void AugmentArchSpec(ArchSpec &spec);

source/API/SBDebugger.cpp
698

Is arch_name here actually a triple? If so, is it worth renaming this variable?

source/API/SBInstruction.cpp
263

Why does this one use HostInfo instead of Platform?

source/Core/ArchSpec.cpp
888–889

Should this function be marked const?

source/Host/common/HostInfoBase.cpp
257–261

I don't *think* this is very performance sensitive, but it's unfortunate that this ends up calling normalize twice. If there's a clean way to re-write it that would probably be nice.

source/Target/Platform.cpp
975–976

Same here, this method ends up normalizing twice, once inside of ContainsMoreThanArch and once inside of this function.

984

Can you early-return here?

986–991

Are these cases even possible? Why would the vendor and os ever be empty? I thought only the environment could be empty.

labath added inline comments.Oct 27 2017, 2:23 PM
source/Target/Platform.cpp
986–991

It is possible, because in some cases, we actually only specify the architecture in calls to this function (see the arch_name comment above). There (I think) its only purpose is to disambiguate which slice in a fat binary are you talking about (and in that case, you don't really need to specify anything other than an architecture).

However, in that case, it is true that what we are passing is not really a triple.

zturner added inline comments.Oct 27 2017, 2:25 PM
source/Target/Platform.cpp
986–991

Right, but you are specifically checking against the normalized triple. I'm pretty sure Triple::normalize will never set these to empty strings. Maybe I'm wrong though (I'm actually about to test)

zturner added inline comments.Oct 27 2017, 2:27 PM
source/Target/Platform.cpp
986–991

Huh. Ok, I guess you're right. The unit tests have this check:

EXPECT_EQ("i386", Triple::normalize("i386"));

I assumed that would return i386-unknown-unknown since they are not considered "optional" components.

labath marked 3 inline comments as done.Oct 27 2017, 3:42 PM
labath added inline comments.
include/lldb/Target/Platform.h
524

Unfortunately it's not possible, as it ends up calling a couple of other function, which are not const, and some of them are virtual.

I don't think the other signature would be very practical, as all the present uses pass in a string, and they don't have an ArchSpec around.

source/API/SBDebugger.cpp
698

As discussed, this is probably expected to only be an architecture name (although, nothing is stopping it from being a triple).

source/API/SBInstruction.cpp
263

Since it is hard-coding a null platform, I figured it is best to be explicit that it is going to pick up data from the host.

As to why is it doing that, I have no idea, but there doesn't seem to be an easy way to fetch a platform instance here.

source/Core/ArchSpec.cpp
888–889

This function is static.

source/Host/common/HostInfoBase.cpp
257–261

I've made the function assume that the incoming triple in normalized. That way, we can just normalize once here, and then pass the normalized triple. I've also inverted the function name to ContainsOnlyArch which is slightly clearer, I think.

labath updated this revision to Diff 120712.Oct 27 2017, 3:43 PM

Address review comments

zturner added inline comments.Oct 27 2017, 3:48 PM
source/Core/ArchSpec.cpp
890

Should you also add !triple.getArchName().empty() to the condition? Also, perhaps name this parameter normalized_triple to document the expectation to the caller.

labath updated this revision to Diff 120714.Oct 27 2017, 3:59 PM

rename argument to normalized_triple, check that the architecture is not empty.

zturner accepted this revision.Oct 27 2017, 4:01 PM
This revision is now accepted and ready to land.Oct 27 2017, 4:01 PM
This revision was automatically updated to reflect the committed changes.
include/lldb/Target/Platform.h