This is an archive of the discontinued LLVM Phabricator instance.

Introduce code_model macros
ClosedPublic

Authored by tamur on Oct 4 2018, 6:29 PM.

Details

Summary

gcc defines macros such as __code_model_small_ based on the user passed command line flag -mcmodel. clang accepts a flag with the same name and similar effects, but does not generate any macro that the user can use. This cl narrows the gap between gcc and clang behaviour.

However, achieving full compatibility with gcc is not trivial: The set of valid values for mcmodel in gcc and clang are not equal. Also, gcc defines different macros for different architectures. In this cl, we only tackle an easy part of the problem and define the macro only for x64 architecture. When the user does not specify a mcmodel, the macro for small code model is produced, as is the case with gcc.

Diff Detail

Repository
rL LLVM

Event Timeline

tamur created this revision.Oct 4 2018, 6:29 PM
MaskRay added inline comments.Oct 5 2018, 10:42 AM
include/clang/Basic/TargetOptions.h
72 ↗(On Diff #168427)

Model -> CodeModel::Model because there are Model in other namespaces

namespace Reloc {
enum Model { Static, PIC_, DynamicNoPIC, ROPI, RWPI, ROPI_RWPI };
}

// Code model types.
namespace CodeModel {
  // Sync changes with CodeGenCWrappers.h.
enum Model { Tiny, Small, Kernel, Medium, Large };
}
lib/Basic/Targets/X86.cpp
866 ↗(On Diff #168427)

The comment // For compatibility with gcc. needs changing.

Small code model is the fastest and expected to be suitable for vast majority of programs.

It is chosen here:

https://github.com/llvm-mirror/llvm/tree/master/lib/Target/X86/X86TargetMachine.cpp#L208

static CodeModel::Model getEffectiveCodeModel(Optional<CodeModel::Model> CM,

                                            bool JIT, bool Is64Bit) {
if (CM)
  return *CM;
if (JIT)
  return Is64Bit ? CodeModel::Large : CodeModel::Small;
return CodeModel::Small;

}

MaskRay added inline comments.Oct 5 2018, 10:53 AM
test/Preprocessor/init.c
7992 ↗(On Diff #168427)
tamur updated this revision to Diff 168494.Oct 5 2018, 11:16 AM
tamur marked 3 inline comments as done.
MaskRay added inline comments.Oct 5 2018, 5:54 PM
lib/Basic/Targets/X86.cpp
865 ↗(On Diff #168494)

I'm not sure if the comment is useful here... or you can comment that this piece of code should be consistent with X86TargetMachine.cpp#L208

tamur updated this revision to Diff 168700.Oct 8 2018, 12:27 PM
tamur added inline comments.Oct 8 2018, 12:30 PM
lib/Basic/Targets/X86.cpp
865 ↗(On Diff #168494)

I removed the comment, Done. What I had wanted was to explain why we have a code_model_small_ macro, not e.g. code_model_default_ macro, but maybe it will be obvious to future readers.

MaskRay accepted this revision.Oct 8 2018, 1:15 PM
This revision is now accepted and ready to land.Oct 8 2018, 1:15 PM
This revision was automatically updated to reflect the committed changes.

LGTM as well. Thanks!