new predefined macros to expose target triple values?

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

new predefined macros to expose target triple values?

Robinson, Paul via cfe-dev
I wondering if anyone besides Apple would be interested in having predefined macros to identify the target OS and environment.

Background: Over the last few years, Apple has added several new platforms (tvOS and watchOS) as well as simulator variants for those, and we’ve accumulated a fair bit of complexity by adding Clang support for those platforms with a copy-and-paste approach. The -mmacosx-version-min option worked well when there was only one Apple platform, but it’s not so great now that we have a lot of -m*-version-min options. We’re trying to move toward the more standard approach of using the -target option to specify the target triple, including the OS version. Akira added support for that earlier (Clang r307982) and we’ve started moving toward identifying simulator targets via the environment field of the triple (LLVM r316380). Related to that, we also need a way to identify via predefined macros what target we’re building for. We recently added the __APPLE_EMBEDDED_SIMULATOR__ macro to distinguish the simulator targets (although it is currently not working when you use the -target option alone), but we don’t have anything to distinguish iOS vs. tvOS vs. watchOS.

It occurred to me that we can do this in a very general way by exposing the OS and Environment fields of the target triple directly in predefined macros. (We could do the same for the Arch and Vendor fields if anyone has a use for those.)

We could add these new macros only for Darwin targets, but since they are not at all Darwin-specific, what do you all think of adding them for all targets?

To be specific, here’s the change that I’m suggesting. (I’ll put it up for a proper review with a testcase if there is positive feedback for doing it this way.)

diff --git lib/Frontend/InitPreprocessor.cpp lib/Frontend/InitPreprocessor.cpp
index 67c1faddc3..c1dfb39c37 100644
--- lib/Frontend/InitPreprocessor.cpp
+++ lib/Frontend/InitPreprocessor.cpp
@@ -713,6 +713,14 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
 
   // Initialize target-specific preprocessor defines.
 
+  // Identify the OS and Environment fields of the target triple.
+  Builder.defineMacro("__TARGET_OS__",
+                      TI.getTriple().getOSTypeName(TI.getTriple().getOS()));
+  if (TI.getTriple().hasEnvironment()) {
+    Builder.defineMacro("__TARGET_ENVIRONMENT__",
+      TI.getTriple().getEnvironmentTypeName(TI.getTriple().getEnvironment()));
+  }
+
   // __BYTE_ORDER__ was added in GCC 4.6. It's analogous
   // to the macro __BYTE_ORDER (no trailing underscores)
   // from glibc's <endian.h> header.

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: new predefined macros to expose target triple values?

Robinson, Paul via cfe-dev
On Thu, Nov 30, 2017 at 5:15 PM, Bob Wilson via cfe-dev <[hidden email]> wrote:
I wondering if anyone besides Apple would be interested in having predefined macros to identify the target OS and environment.

Background: Over the last few years, Apple has added several new platforms (tvOS and watchOS) as well as simulator variants for those, and we’ve accumulated a fair bit of complexity by adding Clang support for those platforms with a copy-and-paste approach. The -mmacosx-version-min option worked well when there was only one Apple platform, but it’s not so great now that we have a lot of -m*-version-min options. We’re trying to move toward the more standard approach of using the -target option to specify the target triple, including the OS version. Akira added support for that earlier (Clang r307982) and we’ve started moving toward identifying simulator targets via the environment field of the triple (LLVM r316380). Related to that, we also need a way to identify via predefined macros what target we’re building for. We recently added the __APPLE_EMBEDDED_SIMULATOR__ macro to distinguish the simulator targets (although it is currently not working when you use the -target option alone), but we don’t have anything to distinguish iOS vs. tvOS vs. watchOS.

It occurred to me that we can do this in a very general way by exposing the OS and Environment fields of the target triple directly in predefined macros. (We could do the same for the Arch and Vendor fields if anyone has a use for those.)

+1 on this.

Arch is exposed, although possibly with a different spelling (e.g. `__powerpc__`).  The vendor is also usually exposed in most toolchains (e.g. `__APPLE__`, or `__INTEL__` from ICC), but is not done in a generalized manner, and is something which I had intended to do as a cleanup.  If you can beat me to it, great!  The OS is already exposed (e.g. `__linux__`), but again suffers the problem of not being done in a generalized manner.
 
We could add these new macros only for Darwin targets, but since they are not at all Darwin-specific, what do you all think of adding them for all targets?

I think all the targets is pretty reasonable.  This actually came up a while ago, but we never went down this path for libc++.  Right now, libc++ is able to build with the itanium ABI and the Microsoft ABI on Windows.  Differentiating which ABI we are using is currently not possible, but access to the environment would solve the problem.

To be specific, here’s the change that I’m suggesting. (I’ll put it up for a proper review with a testcase if there is positive feedback for doing it this way.)

Most of the triple is made accessible, the environment is the one piece which is not, and I think it would be nice to have a uniform way of inspecting this information.

I believe that we could keep the existing architecture macros, uniformly expose vendor as `__<upper case vendor>__`.  The OS could be exposed uniformly as `__<lower case os>__`.  The environment, since there is no real precedent, we can bike shed (fuchsia seems to be a popular color these days :-p), but no strong opinions on that.  I believe if we expose the environment, all 9 pieces of the triple should be visible.

[arch][subarch][endian]-[vendor]-[os]-[environment][abi][object format][version]
 
diff --git lib/Frontend/InitPreprocessor.cpp lib/Frontend/InitPreprocessor.cpp
index 67c1faddc3..c1dfb39c37 100644
--- lib/Frontend/InitPreprocessor.cpp
+++ lib/Frontend/InitPreprocessor.cpp
@@ -713,6 +713,14 @@ static void InitializePredefinedMacros(const TargetInfo &TI,

   // Initialize target-specific preprocessor defines.

+  // Identify the OS and Environment fields of the target triple.
+  Builder.defineMacro("__TARGET_OS__",
+                      TI.getTriple().getOSTypeName(TI.getTriple().getOS()));
+  if (TI.getTriple().hasEnvironment()) {
+    Builder.defineMacro("__TARGET_ENVIRONMENT__",
+      TI.getTriple().getEnvironmentTypeName(TI.getTriple().getEnvironment()));
+  }
+
   // __BYTE_ORDER__ was added in GCC 4.6. It's analogous
   // to the macro __BYTE_ORDER (no trailing underscores)
   // from glibc's <endian.h> header.
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

--
Saleem Abdulrasool
compnerd (at) compnerd (dot) org

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: new predefined macros to expose target triple values?

Robinson, Paul via cfe-dev

On Dec 1, 2017, at 8:09 AM, Saleem Abdulrasool <[hidden email]> wrote:

On Thu, Nov 30, 2017 at 5:15 PM, Bob Wilson via cfe-dev <[hidden email]> wrote:
I wondering if anyone besides Apple would be interested in having predefined macros to identify the target OS and environment.

Background: Over the last few years, Apple has added several new platforms (tvOS and watchOS) as well as simulator variants for those, and we’ve accumulated a fair bit of complexity by adding Clang support for those platforms with a copy-and-paste approach. The -mmacosx-version-min option worked well when there was only one Apple platform, but it’s not so great now that we have a lot of -m*-version-min options. We’re trying to move toward the more standard approach of using the -target option to specify the target triple, including the OS version. Akira added support for that earlier (Clang r307982) and we’ve started moving toward identifying simulator targets via the environment field of the triple (LLVM r316380). Related to that, we also need a way to identify via predefined macros what target we’re building for. We recently added the __APPLE_EMBEDDED_SIMULATOR__ macro to distinguish the simulator targets (although it is currently not working when you use the -target option alone), but we don’t have anything to distinguish iOS vs. tvOS vs. watchOS.

It occurred to me that we can do this in a very general way by exposing the OS and Environment fields of the target triple directly in predefined macros. (We could do the same for the Arch and Vendor fields if anyone has a use for those.)

+1 on this.

Arch is exposed, although possibly with a different spelling (e.g. `__powerpc__`).  The vendor is also usually exposed in most toolchains (e.g. `__APPLE__`, or `__INTEL__` from ICC), but is not done in a generalized manner, and is something which I had intended to do as a cleanup.  If you can beat me to it, great!  The OS is already exposed (e.g. `__linux__`), but again suffers the problem of not being done in a generalized manner.
 
We could add these new macros only for Darwin targets, but since they are not at all Darwin-specific, what do you all think of adding them for all targets?

I think all the targets is pretty reasonable.  This actually came up a while ago, but we never went down this path for libc++.  Right now, libc++ is able to build with the itanium ABI and the Microsoft ABI on Windows.  Differentiating which ABI we are using is currently not possible, but access to the environment would solve the problem.

To be specific, here’s the change that I’m suggesting. (I’ll put it up for a proper review with a testcase if there is positive feedback for doing it this way.)

Most of the triple is made accessible, the environment is the one piece which is not, and I think it would be nice to have a uniform way of inspecting this information.

I believe that we could keep the existing architecture macros, uniformly expose vendor as `__<upper case vendor>__`.  The OS could be exposed uniformly as `__<lower case os>__`.  The environment, since there is no real precedent, we can bike shed (fuchsia seems to be a popular color these days :-p), but no strong opinions on that.  I believe if we expose the environment, all 9 pieces of the triple should be visible.

This is different than what I was proposing here, but you made me realize the fatal flaw in my proposal: there isn’t a good way to do string comparisons in preprocessor conditionals. My thought was that we could write things like:

#if __TARGET_ENVIRONMENT__ == simulator

for code that is specific to the iOS simulator (for example), but of course that doesn’t work.

Alternatively, we could adopt a convention of prefixing the macro names with the triple component name, e.g.:

__TARGET_ENVIRONMENT_SIMULATOR__
__TARGET_OS_TVOS__
etc.

instead of the raw values:

__SIMULATOR__
__TVOS__

It would make sense to me to add the __TARGET-prefixed versions for all targets, but if we do the raw values I would be inclined to do them selectively for platforms that I understand and can test. I slightly prefer the prefixed versions — they give context to show that the macros are about the environment or OS. On the other hand, I suppose they would be a departure from the existing convention with macros like __APPLE__ and __ELF__.

Opinions?


[arch][subarch][endian]-[vendor]-[os]-[environment][abi][object format][version]
 
diff --git lib/Frontend/InitPreprocessor.cpp lib/Frontend/InitPreprocessor.cpp
index 67c1faddc3..c1dfb39c37 100644
--- lib/Frontend/InitPreprocessor.cpp
+++ lib/Frontend/InitPreprocessor.cpp
@@ -713,6 +713,14 @@ static void InitializePredefinedMacros(const TargetInfo &TI,

   // Initialize target-specific preprocessor defines.

+  // Identify the OS and Environment fields of the target triple.
+  Builder.defineMacro("__TARGET_OS__",
+                      TI.getTriple().getOSTypeName(TI.getTriple().getOS()));
+  if (TI.getTriple().hasEnvironment()) {
+    Builder.defineMacro("__TARGET_ENVIRONMENT__",
+      TI.getTriple().getEnvironmentTypeName(TI.getTriple().getEnvironment()));
+  }
+
   // __BYTE_ORDER__ was added in GCC 4.6. It's analogous
   // to the macro __BYTE_ORDER (no trailing underscores)
   // from glibc's <endian.h> header.
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

-- 
Saleem Abdulrasool
compnerd (at) compnerd (dot) org


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: new predefined macros to expose target triple values?

Robinson, Paul via cfe-dev
On Fri, Dec 1, 2017 at 2:46 PM, Bob Wilson <[hidden email]> wrote:

On Dec 1, 2017, at 8:09 AM, Saleem Abdulrasool <[hidden email]> wrote:

On Thu, Nov 30, 2017 at 5:15 PM, Bob Wilson via cfe-dev <[hidden email]> wrote:
I wondering if anyone besides Apple would be interested in having predefined macros to identify the target OS and environment.

Background: Over the last few years, Apple has added several new platforms (tvOS and watchOS) as well as simulator variants for those, and we’ve accumulated a fair bit of complexity by adding Clang support for those platforms with a copy-and-paste approach. The -mmacosx-version-min option worked well when there was only one Apple platform, but it’s not so great now that we have a lot of -m*-version-min options. We’re trying to move toward the more standard approach of using the -target option to specify the target triple, including the OS version. Akira added support for that earlier (Clang r307982) and we’ve started moving toward identifying simulator targets via the environment field of the triple (LLVM r316380). Related to that, we also need a way to identify via predefined macros what target we’re building for. We recently added the __APPLE_EMBEDDED_SIMULATOR__ macro to distinguish the simulator targets (although it is currently not working when you use the -target option alone), but we don’t have anything to distinguish iOS vs. tvOS vs. watchOS.

It occurred to me that we can do this in a very general way by exposing the OS and Environment fields of the target triple directly in predefined macros. (We could do the same for the Arch and Vendor fields if anyone has a use for those.)

+1 on this.

Arch is exposed, although possibly with a different spelling (e.g. `__powerpc__`).  The vendor is also usually exposed in most toolchains (e.g. `__APPLE__`, or `__INTEL__` from ICC), but is not done in a generalized manner, and is something which I had intended to do as a cleanup.  If you can beat me to it, great!  The OS is already exposed (e.g. `__linux__`), but again suffers the problem of not being done in a generalized manner.
 
We could add these new macros only for Darwin targets, but since they are not at all Darwin-specific, what do you all think of adding them for all targets?

I think all the targets is pretty reasonable.  This actually came up a while ago, but we never went down this path for libc++.  Right now, libc++ is able to build with the itanium ABI and the Microsoft ABI on Windows.  Differentiating which ABI we are using is currently not possible, but access to the environment would solve the problem.

To be specific, here’s the change that I’m suggesting. (I’ll put it up for a proper review with a testcase if there is positive feedback for doing it this way.)

Most of the triple is made accessible, the environment is the one piece which is not, and I think it would be nice to have a uniform way of inspecting this information.

I believe that we could keep the existing architecture macros, uniformly expose vendor as `__<upper case vendor>__`.  The OS could be exposed uniformly as `__<lower case os>__`.  The environment, since there is no real precedent, we can bike shed (fuchsia seems to be a popular color these days :-p), but no strong opinions on that.  I believe if we expose the environment, all 9 pieces of the triple should be visible.

This is different than what I was proposing here, but you made me realize the fatal flaw in my proposal: there isn’t a good way to do string comparisons in preprocessor conditionals. My thought was that we could write things like:

#if __TARGET_ENVIRONMENT__ == simulator

for code that is specific to the iOS simulator (for example), but of course that doesn’t work.

Yeah, its unfortunate that that pattern isn't really feasible.
 
Alternatively, we could adopt a convention of prefixing the macro names with the triple component name, e.g.:

__TARGET_ENVIRONMENT_SIMULATOR__
__TARGET_OS_TVOS__
etc.

instead of the raw values:

__SIMULATOR__
__TVOS__

It would make sense to me to add the __TARGET-prefixed versions for all targets, but if we do the raw values I would be inclined to do them selectively for platforms that I understand and can test. I slightly prefer the prefixed versions — they give context to show that the macros are about the environment or OS. On the other hand, I suppose they would be a departure from the existing convention with macros like __APPLE__ and __ELF__.

I actually prefer the un-prefixed versions.  I can appreciate the additional context, but, it would be rather grating from the perspective of combining the old and new versions.  I could be convinced for the prefixed environment macro as there isn't as strong of a precedent there (there is only android which has an environment mapping macro that I can think of).

Opinions?

One other option is to provide a function-like macros to accomplish this.  Similar to `__has_feature`, we could expose:

- `__is_auxiliary_os(os)`
- `__is_target_os(os)`
- `__is_auxiliary_environment(environment)`
- `__is_target_environment(environment)`
- `__is_auxiliary_vendor(vendor)`
- `__is_target_vendor(vendor)`

This would be cleaner overall I feel, but a much more radical departure for these cases.

[arch][subarch][endian]-[vendor]-[os]-[environment][abi][object format][version]
 
diff --git lib/Frontend/InitPreprocessor.cpp lib/Frontend/InitPreprocessor.cpp
index 67c1faddc3..c1dfb39c37 100644
--- lib/Frontend/InitPreprocessor.cpp
+++ lib/Frontend/InitPreprocessor.cpp
@@ -713,6 +713,14 @@ static void InitializePredefinedMacros(const TargetInfo &TI,

   // Initialize target-specific preprocessor defines.

+  // Identify the OS and Environment fields of the target triple.
+  Builder.defineMacro("__TARGET_OS__",
+                      TI.getTriple().getOSTypeName(TI.getTriple().getOS()));
+  if (TI.getTriple().hasEnvironment()) {
+    Builder.defineMacro("__TARGET_ENVIRONMENT__",
+      TI.getTriple().getEnvironmentTypeName(TI.getTriple().getEnvironment()));
+  }
+
   // __BYTE_ORDER__ was added in GCC 4.6. It's analogous
   // to the macro __BYTE_ORDER (no trailing underscores)
   // from glibc's <endian.h> header.
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

-- 
Saleem Abdulrasool
compnerd (at) compnerd (dot) org




--
Saleem Abdulrasool
compnerd (at) compnerd (dot) org

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: new predefined macros to expose target triple values?

Robinson, Paul via cfe-dev


On Dec 3, 2017, at 10:14, Saleem Abdulrasool via cfe-dev <[hidden email]> wrote:

On Fri, Dec 1, 2017 at 2:46 PM, Bob Wilson <[hidden email]> wrote:

On Dec 1, 2017, at 8:09 AM, Saleem Abdulrasool <[hidden email]> wrote:

On Thu, Nov 30, 2017 at 5:15 PM, Bob Wilson via cfe-dev <[hidden email]> wrote:
I wondering if anyone besides Apple would be interested in having predefined macros to identify the target OS and environment.

Background: Over the last few years, Apple has added several new platforms (tvOS and watchOS) as well as simulator variants for those, and we’ve accumulated a fair bit of complexity by adding Clang support for those platforms with a copy-and-paste approach. The -mmacosx-version-min option worked well when there was only one Apple platform, but it’s not so great now that we have a lot of -m*-version-min options. We’re trying to move toward the more standard approach of using the -target option to specify the target triple, including the OS version. Akira added support for that earlier (Clang r307982) and we’ve started moving toward identifying simulator targets via the environment field of the triple (LLVM r316380). Related to that, we also need a way to identify via predefined macros what target we’re building for. We recently added the __APPLE_EMBEDDED_SIMULATOR__ macro to distinguish the simulator targets (although it is currently not working when you use the -target option alone), but we don’t have anything to distinguish iOS vs. tvOS vs. watchOS.

It occurred to me that we can do this in a very general way by exposing the OS and Environment fields of the target triple directly in predefined macros. (We could do the same for the Arch and Vendor fields if anyone has a use for those.)

+1 on this.

Arch is exposed, although possibly with a different spelling (e.g. `__powerpc__`).  The vendor is also usually exposed in most toolchains (e.g. `__APPLE__`, or `__INTEL__` from ICC), but is not done in a generalized manner, and is something which I had intended to do as a cleanup.  If you can beat me to it, great!  The OS is already exposed (e.g. `__linux__`), but again suffers the problem of not being done in a generalized manner.
 
We could add these new macros only for Darwin targets, but since they are not at all Darwin-specific, what do you all think of adding them for all targets?

I think all the targets is pretty reasonable.  This actually came up a while ago, but we never went down this path for libc++.  Right now, libc++ is able to build with the itanium ABI and the Microsoft ABI on Windows.  Differentiating which ABI we are using is currently not possible, but access to the environment would solve the problem.

To be specific, here’s the change that I’m suggesting. (I’ll put it up for a proper review with a testcase if there is positive feedback for doing it this way.)

Most of the triple is made accessible, the environment is the one piece which is not, and I think it would be nice to have a uniform way of inspecting this information.

I believe that we could keep the existing architecture macros, uniformly expose vendor as `__<upper case vendor>__`.  The OS could be exposed uniformly as `__<lower case os>__`.  The environment, since there is no real precedent, we can bike shed (fuchsia seems to be a popular color these days :-p), but no strong opinions on that.  I believe if we expose the environment, all 9 pieces of the triple should be visible.

This is different than what I was proposing here, but you made me realize the fatal flaw in my proposal: there isn’t a good way to do string comparisons in preprocessor conditionals. My thought was that we could write things like:

#if __TARGET_ENVIRONMENT__ == simulator

for code that is specific to the iOS simulator (for example), but of course that doesn’t work.

Yeah, its unfortunate that that pattern isn't really feasible.
 
Alternatively, we could adopt a convention of prefixing the macro names with the triple component name, e.g.:

__TARGET_ENVIRONMENT_SIMULATOR__
__TARGET_OS_TVOS__
etc.

instead of the raw values:

__SIMULATOR__
__TVOS__

It would make sense to me to add the __TARGET-prefixed versions for all targets, but if we do the raw values I would be inclined to do them selectively for platforms that I understand and can test. I slightly prefer the prefixed versions — they give context to show that the macros are about the environment or OS. On the other hand, I suppose they would be a departure from the existing convention with macros like __APPLE__ and __ELF__.

I actually prefer the un-prefixed versions.  I can appreciate the additional context, but, it would be rather grating from the perspective of combining the old and new versions.  I could be convinced for the prefixed environment macro as there isn't as strong of a precedent there (there is only android which has an environment mapping macro that I can think of).

I'd worry that the non-prefixed versions would stomp on macros used by headers also provided elsewhere in the implementation (e.g., system headers).

I'm not sure I understand the concern with the prefixed versions.  For x86_64-apple-ios-simulator, I imagine we'd have:
```
__X86_64__ // or whatever the current one is
__APPLE__
__TARGET_ARCH_X86_64__
__TARGET_VENDOR_APPLE__
__TARGET_OS_IOS__
__TARGET_ENVIRONMENT_SIMULATOR__
```

I imagine legacy header code would use the old, un-prefixed macros, and new header code would use the prefixed macros.  It seems unlikely that much code would have logic depending on a mix of them.

Opinions?

One other option is to provide a function-like macros to accomplish this.  Similar to `__has_feature`, we could expose:

- `__is_auxiliary_os(os)`
- `__is_target_os(os)`
- `__is_auxiliary_environment(environment)`
- `__is_target_environment(environment)`
- `__is_auxiliary_vendor(vendor)`
- `__is_target_vendor(vendor)`

This would be cleaner overall I feel, but a much more radical departure for these cases.

FWIW, I like this idea quite a bit.


[arch][subarch][endian]-[vendor]-[os]-[environment][abi][object format][version]
 
diff --git lib/Frontend/InitPreprocessor.cpp lib/Frontend/InitPreprocessor.cpp
index 67c1faddc3..c1dfb39c37 100644
--- lib/Frontend/InitPreprocessor.cpp
+++ lib/Frontend/InitPreprocessor.cpp
@@ -713,6 +713,14 @@ static void InitializePredefinedMacros(const TargetInfo &TI,

   // Initialize target-specific preprocessor defines.

+  // Identify the OS and Environment fields of the target triple.
+  Builder.defineMacro("__TARGET_OS__",
+                      TI.getTriple().getOSTypeName(TI.getTriple().getOS()));
+  if (TI.getTriple().hasEnvironment()) {
+    Builder.defineMacro("__TARGET_ENVIRONMENT__",
+      TI.getTriple().getEnvironmentTypeName(TI.getTriple().getEnvironment()));
+  }
+
   // __BYTE_ORDER__ was added in GCC 4.6. It's analogous
   // to the macro __BYTE_ORDER (no trailing underscores)
   // from glibc's <endian.h> header.
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

-- 
Saleem Abdulrasool
compnerd (at) compnerd (dot) org




-- 
Saleem Abdulrasool
compnerd (at) compnerd (dot) org
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: new predefined macros to expose target triple values?

Robinson, Paul via cfe-dev


On Dec 6, 2017, at 10:25, Duncan P. N. Exon Smith <[hidden email]> wrote:



On Dec 3, 2017, at 10:14, Saleem Abdulrasool via cfe-dev <[hidden email]> wrote:

On Fri, Dec 1, 2017 at 2:46 PM, Bob Wilson <[hidden email]> wrote:

On Dec 1, 2017, at 8:09 AM, Saleem Abdulrasool <[hidden email]> wrote:

On Thu, Nov 30, 2017 at 5:15 PM, Bob Wilson via cfe-dev <[hidden email]> wrote:
I wondering if anyone besides Apple would be interested in having predefined macros to identify the target OS and environment.

Background: Over the last few years, Apple has added several new platforms (tvOS and watchOS) as well as simulator variants for those, and we’ve accumulated a fair bit of complexity by adding Clang support for those platforms with a copy-and-paste approach. The -mmacosx-version-min option worked well when there was only one Apple platform, but it’s not so great now that we have a lot of -m*-version-min options. We’re trying to move toward the more standard approach of using the -target option to specify the target triple, including the OS version. Akira added support for that earlier (Clang r307982) and we’ve started moving toward identifying simulator targets via the environment field of the triple (LLVM r316380). Related to that, we also need a way to identify via predefined macros what target we’re building for. We recently added the __APPLE_EMBEDDED_SIMULATOR__ macro to distinguish the simulator targets (although it is currently not working when you use the -target option alone), but we don’t have anything to distinguish iOS vs. tvOS vs. watchOS.

It occurred to me that we can do this in a very general way by exposing the OS and Environment fields of the target triple directly in predefined macros. (We could do the same for the Arch and Vendor fields if anyone has a use for those.)

+1 on this.

Arch is exposed, although possibly with a different spelling (e.g. `__powerpc__`).  The vendor is also usually exposed in most toolchains (e.g. `__APPLE__`, or `__INTEL__` from ICC), but is not done in a generalized manner, and is something which I had intended to do as a cleanup.  If you can beat me to it, great!  The OS is already exposed (e.g. `__linux__`), but again suffers the problem of not being done in a generalized manner.
 
We could add these new macros only for Darwin targets, but since they are not at all Darwin-specific, what do you all think of adding them for all targets?

I think all the targets is pretty reasonable.  This actually came up a while ago, but we never went down this path for libc++.  Right now, libc++ is able to build with the itanium ABI and the Microsoft ABI on Windows.  Differentiating which ABI we are using is currently not possible, but access to the environment would solve the problem.

To be specific, here’s the change that I’m suggesting. (I’ll put it up for a proper review with a testcase if there is positive feedback for doing it this way.)

Most of the triple is made accessible, the environment is the one piece which is not, and I think it would be nice to have a uniform way of inspecting this information.

I believe that we could keep the existing architecture macros, uniformly expose vendor as `__<upper case vendor>__`.  The OS could be exposed uniformly as `__<lower case os>__`.  The environment, since there is no real precedent, we can bike shed (fuchsia seems to be a popular color these days :-p), but no strong opinions on that.  I believe if we expose the environment, all 9 pieces of the triple should be visible.

This is different than what I was proposing here, but you made me realize the fatal flaw in my proposal: there isn’t a good way to do string comparisons in preprocessor conditionals. My thought was that we could write things like:

#if __TARGET_ENVIRONMENT__ == simulator

for code that is specific to the iOS simulator (for example), but of course that doesn’t work.

Yeah, its unfortunate that that pattern isn't really feasible.
 
Alternatively, we could adopt a convention of prefixing the macro names with the triple component name, e.g.:

__TARGET_ENVIRONMENT_SIMULATOR__
__TARGET_OS_TVOS__
etc.

instead of the raw values:

__SIMULATOR__
__TVOS__

It would make sense to me to add the __TARGET-prefixed versions for all targets, but if we do the raw values I would be inclined to do them selectively for platforms that I understand and can test. I slightly prefer the prefixed versions — they give context to show that the macros are about the environment or OS. On the other hand, I suppose they would be a departure from the existing convention with macros like __APPLE__ and __ELF__.

I actually prefer the un-prefixed versions.  I can appreciate the additional context, but, it would be rather grating from the perspective of combining the old and new versions.  I could be convinced for the prefixed environment macro as there isn't as strong of a precedent there (there is only android which has an environment mapping macro that I can think of).

I'd worry that the non-prefixed versions would stomp on macros used by headers also provided elsewhere in the implementation (e.g., system headers).

I'm not sure I understand the concern with the prefixed versions.  For x86_64-apple-ios-simulator, I imagine we'd have:
```
__X86_64__ // or whatever the current one is
__APPLE__
__TARGET_ARCH_X86_64__
__TARGET_VENDOR_APPLE__
__TARGET_OS_IOS__
__TARGET_ENVIRONMENT_SIMULATOR__
```

I imagine legacy header code would use the old, un-prefixed macros, and new header code would use the prefixed macros.  It seems unlikely that much code would have logic depending on a mix of them.

Opinions?

One other option is to provide a function-like macros to accomplish this.  Similar to `__has_feature`, we could expose:

- `__is_auxiliary_os(os)`
- `__is_target_os(os)`
- `__is_auxiliary_environment(environment)`
- `__is_target_environment(environment)`
- `__is_auxiliary_vendor(vendor)`
- `__is_target_vendor(vendor)`

This would be cleaner overall I feel, but a much more radical departure for these cases.

FWIW, I like this idea quite a bit.

Although, maybe I just like it because I'm used to function calls.  It is a fairly radical departure.  It might be best to add a clean set of macros while we ponder what the right intrinsic design might be; we could always add them later.

BTW, you seem to be referencing an auxiliary target.  What's that about?


[arch][subarch][endian]-[vendor]-[os]-[environment][abi][object format][version]
 
diff --git lib/Frontend/InitPreprocessor.cpp lib/Frontend/InitPreprocessor.cpp
index 67c1faddc3..c1dfb39c37 100644
--- lib/Frontend/InitPreprocessor.cpp
+++ lib/Frontend/InitPreprocessor.cpp
@@ -713,6 +713,14 @@ static void InitializePredefinedMacros(const TargetInfo &TI,

   // Initialize target-specific preprocessor defines.

+  // Identify the OS and Environment fields of the target triple.
+  Builder.defineMacro("__TARGET_OS__",
+                      TI.getTriple().getOSTypeName(TI.getTriple().getOS()));
+  if (TI.getTriple().hasEnvironment()) {
+    Builder.defineMacro("__TARGET_ENVIRONMENT__",
+      TI.getTriple().getEnvironmentTypeName(TI.getTriple().getEnvironment()));
+  }
+
   // __BYTE_ORDER__ was added in GCC 4.6. It's analogous
   // to the macro __BYTE_ORDER (no trailing underscores)
   // from glibc's <endian.h> header.
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

-- 
Saleem Abdulrasool
compnerd (at) compnerd (dot) org




-- 
Saleem Abdulrasool
compnerd (at) compnerd (dot) org
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: new predefined macros to expose target triple values?

Robinson, Paul via cfe-dev


On Dec 6, 2017, at 12:10 PM, Duncan P. N. Exon Smith <[hidden email]> wrote:



On Dec 6, 2017, at 10:25, Duncan P. N. Exon Smith <[hidden email]> wrote:



On Dec 3, 2017, at 10:14, Saleem Abdulrasool via cfe-dev <[hidden email]> wrote:

On Fri, Dec 1, 2017 at 2:46 PM, Bob Wilson <[hidden email]> wrote:

On Dec 1, 2017, at 8:09 AM, Saleem Abdulrasool <[hidden email]> wrote:

On Thu, Nov 30, 2017 at 5:15 PM, Bob Wilson via cfe-dev <[hidden email]> wrote:
I wondering if anyone besides Apple would be interested in having predefined macros to identify the target OS and environment.

Background: Over the last few years, Apple has added several new platforms (tvOS and watchOS) as well as simulator variants for those, and we’ve accumulated a fair bit of complexity by adding Clang support for those platforms with a copy-and-paste approach. The -mmacosx-version-min option worked well when there was only one Apple platform, but it’s not so great now that we have a lot of -m*-version-min options. We’re trying to move toward the more standard approach of using the -target option to specify the target triple, including the OS version. Akira added support for that earlier (Clang r307982) and we’ve started moving toward identifying simulator targets via the environment field of the triple (LLVM r316380). Related to that, we also need a way to identify via predefined macros what target we’re building for. We recently added the __APPLE_EMBEDDED_SIMULATOR__ macro to distinguish the simulator targets (although it is currently not working when you use the -target option alone), but we don’t have anything to distinguish iOS vs. tvOS vs. watchOS.

It occurred to me that we can do this in a very general way by exposing the OS and Environment fields of the target triple directly in predefined macros. (We could do the same for the Arch and Vendor fields if anyone has a use for those.)

+1 on this.

Arch is exposed, although possibly with a different spelling (e.g. `__powerpc__`).  The vendor is also usually exposed in most toolchains (e.g. `__APPLE__`, or `__INTEL__` from ICC), but is not done in a generalized manner, and is something which I had intended to do as a cleanup.  If you can beat me to it, great!  The OS is already exposed (e.g. `__linux__`), but again suffers the problem of not being done in a generalized manner.
 
We could add these new macros only for Darwin targets, but since they are not at all Darwin-specific, what do you all think of adding them for all targets?

I think all the targets is pretty reasonable.  This actually came up a while ago, but we never went down this path for libc++.  Right now, libc++ is able to build with the itanium ABI and the Microsoft ABI on Windows.  Differentiating which ABI we are using is currently not possible, but access to the environment would solve the problem.

To be specific, here’s the change that I’m suggesting. (I’ll put it up for a proper review with a testcase if there is positive feedback for doing it this way.)

Most of the triple is made accessible, the environment is the one piece which is not, and I think it would be nice to have a uniform way of inspecting this information.

I believe that we could keep the existing architecture macros, uniformly expose vendor as `__<upper case vendor>__`.  The OS could be exposed uniformly as `__<lower case os>__`.  The environment, since there is no real precedent, we can bike shed (fuchsia seems to be a popular color these days :-p), but no strong opinions on that.  I believe if we expose the environment, all 9 pieces of the triple should be visible.

This is different than what I was proposing here, but you made me realize the fatal flaw in my proposal: there isn’t a good way to do string comparisons in preprocessor conditionals. My thought was that we could write things like:

#if __TARGET_ENVIRONMENT__ == simulator

for code that is specific to the iOS simulator (for example), but of course that doesn’t work.

Yeah, its unfortunate that that pattern isn't really feasible.
 
Alternatively, we could adopt a convention of prefixing the macro names with the triple component name, e.g.:

__TARGET_ENVIRONMENT_SIMULATOR__
__TARGET_OS_TVOS__
etc.

instead of the raw values:

__SIMULATOR__
__TVOS__

It would make sense to me to add the __TARGET-prefixed versions for all targets, but if we do the raw values I would be inclined to do them selectively for platforms that I understand and can test. I slightly prefer the prefixed versions — they give context to show that the macros are about the environment or OS. On the other hand, I suppose they would be a departure from the existing convention with macros like __APPLE__ and __ELF__.

I actually prefer the un-prefixed versions.  I can appreciate the additional context, but, it would be rather grating from the perspective of combining the old and new versions.  I could be convinced for the prefixed environment macro as there isn't as strong of a precedent there (there is only android which has an environment mapping macro that I can think of).

I'd worry that the non-prefixed versions would stomp on macros used by headers also provided elsewhere in the implementation (e.g., system headers).

Ah, but in those cases the headers are already non-conformant and breaking those, while unfortunate, is not entirely unreasonable.  IIRC, `_[A-Z]` is reserved for the implementation and `__` is left for the compiler.  (Yes, I am aware that many people simply follow the herd mentality and use header guards with `__` prefixes, but that is a separate discussion.)


I'm not sure I understand the concern with the prefixed versions.  For x86_64-apple-ios-simulator, I imagine we'd have:
```
__X86_64__ // or whatever the current one is
__APPLE__
__TARGET_ARCH_X86_64__
__TARGET_VENDOR_APPLE__
__TARGET_OS_IOS__
__TARGET_ENVIRONMENT_SIMULATOR__
```

I imagine legacy header code would use the old, un-prefixed macros, and new header code would use the prefixed macros.  It seems unlikely that much code would have logic depending on a mix of them.

Opinions?

One other option is to provide a function-like macros to accomplish this.  Similar to `__has_feature`, we could expose:

- `__is_auxiliary_os(os)`
- `__is_target_os(os)`
- `__is_auxiliary_environment(environment)`
- `__is_target_environment(environment)`
- `__is_auxiliary_vendor(vendor)`
- `__is_target_vendor(vendor)`

This would be cleaner overall I feel, but a much more radical departure for these cases.

FWIW, I like this idea quite a bit.

Although, maybe I just like it because I'm used to function calls.  It is a fairly radical departure.  It might be best to add a clean set of macros while we ponder what the right intrinsic design might be; we could always add them later.

So, Id like to point out that it is only as far a radical departure as clang is from gcc.  We already have `__has_feature`, `__has_extension`, `__is_identifier`, etc which already fit this model.  I believe that extending that model leaves plenty of prior art and will not be odd from a clang perspective, but only from the user perspective where they may be used to using `#if defined(__x86_64__)` rather than `#if __is_target_arch(x86_64)`.

I’m not sure that I agree in this being so radical as to require significantly more effort than the traditional CPP macro approach.  I believe that getting the names correct is important, but we would face similar issues with the preprocessor approach.

BTW, you seem to be referencing an auxiliary target.  What's that about?

Keen eye :-).  It is to point out an additional dimension that would have been forgotten about otherwise.  CUDA compilation has a model where there is the primary target as well as a secondary (auxiliary) target which describes the host environment.


[arch][subarch][endian]-[vendor]-[os]-[environment][abi][object format][version]
 
diff --git lib/Frontend/InitPreprocessor.cpp lib/Frontend/InitPreprocessor.cpp
index 67c1faddc3..c1dfb39c37 100644
--- lib/Frontend/InitPreprocessor.cpp
+++ lib/Frontend/InitPreprocessor.cpp
@@ -713,6 +713,14 @@ static void InitializePredefinedMacros(const TargetInfo &TI,

   // Initialize target-specific preprocessor defines.

+  // Identify the OS and Environment fields of the target triple.
+  Builder.defineMacro("__TARGET_OS__",
+                      TI.getTriple().getOSTypeName(TI.getTriple().getOS()));
+  if (TI.getTriple().hasEnvironment()) {
+    Builder.defineMacro("__TARGET_ENVIRONMENT__",
+      TI.getTriple().getEnvironmentTypeName(TI.getTriple().getEnvironment()));
+  }
+
   // __BYTE_ORDER__ was added in GCC 4.6. It's analogous
   // to the macro __BYTE_ORDER (no trailing underscores)
   // from glibc's <endian.h> header.
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

-- 
Saleem Abdulrasool
compnerd (at) compnerd (dot) org




-- 
Saleem Abdulrasool
compnerd (at) compnerd (dot) org
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: new predefined macros to expose target triple values?

Robinson, Paul via cfe-dev


On Dec 8, 2017, at 20:32, Saleem Abdulrasool <[hidden email]> wrote:



On Dec 6, 2017, at 12:10 PM, Duncan P. N. Exon Smith <[hidden email]> wrote:



On Dec 6, 2017, at 10:25, Duncan P. N. Exon Smith <[hidden email]> wrote:



On Dec 3, 2017, at 10:14, Saleem Abdulrasool via cfe-dev <[hidden email]> wrote:

On Fri, Dec 1, 2017 at 2:46 PM, Bob Wilson <[hidden email]> wrote:

On Dec 1, 2017, at 8:09 AM, Saleem Abdulrasool <[hidden email]> wrote:

On Thu, Nov 30, 2017 at 5:15 PM, Bob Wilson via cfe-dev <[hidden email]> wrote:
I wondering if anyone besides Apple would be interested in having predefined macros to identify the target OS and environment.

Background: Over the last few years, Apple has added several new platforms (tvOS and watchOS) as well as simulator variants for those, and we’ve accumulated a fair bit of complexity by adding Clang support for those platforms with a copy-and-paste approach. The -mmacosx-version-min option worked well when there was only one Apple platform, but it’s not so great now that we have a lot of -m*-version-min options. We’re trying to move toward the more standard approach of using the -target option to specify the target triple, including the OS version. Akira added support for that earlier (Clang r307982) and we’ve started moving toward identifying simulator targets via the environment field of the triple (LLVM r316380). Related to that, we also need a way to identify via predefined macros what target we’re building for. We recently added the __APPLE_EMBEDDED_SIMULATOR__ macro to distinguish the simulator targets (although it is currently not working when you use the -target option alone), but we don’t have anything to distinguish iOS vs. tvOS vs. watchOS.

It occurred to me that we can do this in a very general way by exposing the OS and Environment fields of the target triple directly in predefined macros. (We could do the same for the Arch and Vendor fields if anyone has a use for those.)

+1 on this.

Arch is exposed, although possibly with a different spelling (e.g. `__powerpc__`).  The vendor is also usually exposed in most toolchains (e.g. `__APPLE__`, or `__INTEL__` from ICC), but is not done in a generalized manner, and is something which I had intended to do as a cleanup.  If you can beat me to it, great!  The OS is already exposed (e.g. `__linux__`), but again suffers the problem of not being done in a generalized manner.
 
We could add these new macros only for Darwin targets, but since they are not at all Darwin-specific, what do you all think of adding them for all targets?

I think all the targets is pretty reasonable.  This actually came up a while ago, but we never went down this path for libc++.  Right now, libc++ is able to build with the itanium ABI and the Microsoft ABI on Windows.  Differentiating which ABI we are using is currently not possible, but access to the environment would solve the problem.

To be specific, here’s the change that I’m suggesting. (I’ll put it up for a proper review with a testcase if there is positive feedback for doing it this way.)

Most of the triple is made accessible, the environment is the one piece which is not, and I think it would be nice to have a uniform way of inspecting this information.

I believe that we could keep the existing architecture macros, uniformly expose vendor as `__<upper case vendor>__`.  The OS could be exposed uniformly as `__<lower case os>__`.  The environment, since there is no real precedent, we can bike shed (fuchsia seems to be a popular color these days :-p), but no strong opinions on that.  I believe if we expose the environment, all 9 pieces of the triple should be visible.

This is different than what I was proposing here, but you made me realize the fatal flaw in my proposal: there isn’t a good way to do string comparisons in preprocessor conditionals. My thought was that we could write things like:

#if __TARGET_ENVIRONMENT__ == simulator

for code that is specific to the iOS simulator (for example), but of course that doesn’t work.

Yeah, its unfortunate that that pattern isn't really feasible.
 
Alternatively, we could adopt a convention of prefixing the macro names with the triple component name, e.g.:

__TARGET_ENVIRONMENT_SIMULATOR__
__TARGET_OS_TVOS__
etc.

instead of the raw values:

__SIMULATOR__
__TVOS__

It would make sense to me to add the __TARGET-prefixed versions for all targets, but if we do the raw values I would be inclined to do them selectively for platforms that I understand and can test. I slightly prefer the prefixed versions — they give context to show that the macros are about the environment or OS. On the other hand, I suppose they would be a departure from the existing convention with macros like __APPLE__ and __ELF__.

I actually prefer the un-prefixed versions.  I can appreciate the additional context, but, it would be rather grating from the perspective of combining the old and new versions.  I could be convinced for the prefixed environment macro as there isn't as strong of a precedent there (there is only android which has an environment mapping macro that I can think of).

I'd worry that the non-prefixed versions would stomp on macros used by headers also provided elsewhere in the implementation (e.g., system headers).

Ah, but in those cases the headers are already non-conformant and breaking those, while unfortunate, is not entirely unreasonable.  IIRC, `_[A-Z]` is reserved for the implementation and `__` is left for the compiler.  (Yes, I am aware that many people simply follow the herd mentality and use header guards with `__` prefixes, but that is a separate discussion.)

Right, but the headers elsewhere in the implementation (such as libc or the kernel) are still part of the implementation.  I think you misread my previous comment.


I'm not sure I understand the concern with the prefixed versions.  For x86_64-apple-ios-simulator, I imagine we'd have:
```
__X86_64__ // or whatever the current one is
__APPLE__
__TARGET_ARCH_X86_64__
__TARGET_VENDOR_APPLE__
__TARGET_OS_IOS__
__TARGET_ENVIRONMENT_SIMULATOR__
```

I imagine legacy header code would use the old, un-prefixed macros, and new header code would use the prefixed macros.  It seems unlikely that much code would have logic depending on a mix of them.

Opinions?

One other option is to provide a function-like macros to accomplish this.  Similar to `__has_feature`, we could expose:

- `__is_auxiliary_os(os)`
- `__is_target_os(os)`
- `__is_auxiliary_environment(environment)`
- `__is_target_environment(environment)`
- `__is_auxiliary_vendor(vendor)`
- `__is_target_vendor(vendor)`

This would be cleaner overall I feel, but a much more radical departure for these cases.

FWIW, I like this idea quite a bit.

Although, maybe I just like it because I'm used to function calls.  It is a fairly radical departure.  It might be best to add a clean set of macros while we ponder what the right intrinsic design might be; we could always add them later.

So, Id like to point out that it is only as far a radical departure as clang is from gcc.  We already have `__has_feature`, `__has_extension`, `__is_identifier`, etc which already fit this model.  I believe that extending that model leaves plenty of prior art and will not be odd from a clang perspective, but only from the user perspective where they may be used to using `#if defined(__x86_64__)` rather than `#if __is_target_arch(x86_64)`.

Users would also need a `__has_feature(target_intrinsics)` check.

I’m not sure that I agree in this being so radical as to require significantly more effort than the traditional CPP macro approach.  I believe that getting the names correct is important, but we would face similar issues with the preprocessor approach.

BTW, you seem to be referencing an auxiliary target.  What's that about?

Keen eye :-).  It is to point out an additional dimension that would have been forgotten about otherwise.  CUDA compilation has a model where there is the primary target as well as a secondary (auxiliary) target which describes the host environment.


[arch][subarch][endian]-[vendor]-[os]-[environment][abi][object format][version]
 
diff --git lib/Frontend/InitPreprocessor.cpp lib/Frontend/InitPreprocessor.cpp
index 67c1faddc3..c1dfb39c37 100644
--- lib/Frontend/InitPreprocessor.cpp
+++ lib/Frontend/InitPreprocessor.cpp
@@ -713,6 +713,14 @@ static void InitializePredefinedMacros(const TargetInfo &TI,

   // Initialize target-specific preprocessor defines.

+  // Identify the OS and Environment fields of the target triple.
+  Builder.defineMacro("__TARGET_OS__",
+                      TI.getTriple().getOSTypeName(TI.getTriple().getOS()));
+  if (TI.getTriple().hasEnvironment()) {
+    Builder.defineMacro("__TARGET_ENVIRONMENT__",
+      TI.getTriple().getEnvironmentTypeName(TI.getTriple().getEnvironment()));
+  }
+
   // __BYTE_ORDER__ was added in GCC 4.6. It's analogous
   // to the macro __BYTE_ORDER (no trailing underscores)
   // from glibc's <endian.h> header.
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

-- 
Saleem Abdulrasool
compnerd (at) compnerd (dot) org




-- 
Saleem Abdulrasool
compnerd (at) compnerd (dot) org
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: new predefined macros to expose target triple values?

Robinson, Paul via cfe-dev


On Dec 9, 2017, at 3:04 PM, Duncan P. N. Exon Smith <[hidden email]> wrote:



On Dec 8, 2017, at 20:32, Saleem Abdulrasool <[hidden email]> wrote:



On Dec 6, 2017, at 12:10 PM, Duncan P. N. Exon Smith <[hidden email]> wrote:



On Dec 6, 2017, at 10:25, Duncan P. N. Exon Smith <[hidden email]> wrote:



On Dec 3, 2017, at 10:14, Saleem Abdulrasool via cfe-dev <[hidden email]> wrote:

On Fri, Dec 1, 2017 at 2:46 PM, Bob Wilson <[hidden email]> wrote:

On Dec 1, 2017, at 8:09 AM, Saleem Abdulrasool <[hidden email]> wrote:

On Thu, Nov 30, 2017 at 5:15 PM, Bob Wilson via cfe-dev <[hidden email]> wrote:
I wondering if anyone besides Apple would be interested in having predefined macros to identify the target OS and environment.

Background: Over the last few years, Apple has added several new platforms (tvOS and watchOS) as well as simulator variants for those, and we’ve accumulated a fair bit of complexity by adding Clang support for those platforms with a copy-and-paste approach. The -mmacosx-version-min option worked well when there was only one Apple platform, but it’s not so great now that we have a lot of -m*-version-min options. We’re trying to move toward the more standard approach of using the -target option to specify the target triple, including the OS version. Akira added support for that earlier (Clang r307982) and we’ve started moving toward identifying simulator targets via the environment field of the triple (LLVM r316380). Related to that, we also need a way to identify via predefined macros what target we’re building for. We recently added the __APPLE_EMBEDDED_SIMULATOR__ macro to distinguish the simulator targets (although it is currently not working when you use the -target option alone), but we don’t have anything to distinguish iOS vs. tvOS vs. watchOS.

It occurred to me that we can do this in a very general way by exposing the OS and Environment fields of the target triple directly in predefined macros. (We could do the same for the Arch and Vendor fields if anyone has a use for those.)

+1 on this.

Arch is exposed, although possibly with a different spelling (e.g. `__powerpc__`).  The vendor is also usually exposed in most toolchains (e.g. `__APPLE__`, or `__INTEL__` from ICC), but is not done in a generalized manner, and is something which I had intended to do as a cleanup.  If you can beat me to it, great!  The OS is already exposed (e.g. `__linux__`), but again suffers the problem of not being done in a generalized manner.
 
We could add these new macros only for Darwin targets, but since they are not at all Darwin-specific, what do you all think of adding them for all targets?

I think all the targets is pretty reasonable.  This actually came up a while ago, but we never went down this path for libc++.  Right now, libc++ is able to build with the itanium ABI and the Microsoft ABI on Windows.  Differentiating which ABI we are using is currently not possible, but access to the environment would solve the problem.

To be specific, here’s the change that I’m suggesting. (I’ll put it up for a proper review with a testcase if there is positive feedback for doing it this way.)

Most of the triple is made accessible, the environment is the one piece which is not, and I think it would be nice to have a uniform way of inspecting this information.

I believe that we could keep the existing architecture macros, uniformly expose vendor as `__<upper case vendor>__`.  The OS could be exposed uniformly as `__<lower case os>__`.  The environment, since there is no real precedent, we can bike shed (fuchsia seems to be a popular color these days :-p), but no strong opinions on that.  I believe if we expose the environment, all 9 pieces of the triple should be visible.

This is different than what I was proposing here, but you made me realize the fatal flaw in my proposal: there isn’t a good way to do string comparisons in preprocessor conditionals. My thought was that we could write things like:

#if __TARGET_ENVIRONMENT__ == simulator

for code that is specific to the iOS simulator (for example), but of course that doesn’t work.

Yeah, its unfortunate that that pattern isn't really feasible.
 
Alternatively, we could adopt a convention of prefixing the macro names with the triple component name, e.g.:

__TARGET_ENVIRONMENT_SIMULATOR__
__TARGET_OS_TVOS__
etc.

instead of the raw values:

__SIMULATOR__
__TVOS__

It would make sense to me to add the __TARGET-prefixed versions for all targets, but if we do the raw values I would be inclined to do them selectively for platforms that I understand and can test. I slightly prefer the prefixed versions — they give context to show that the macros are about the environment or OS. On the other hand, I suppose they would be a departure from the existing convention with macros like __APPLE__ and __ELF__.

I actually prefer the un-prefixed versions.  I can appreciate the additional context, but, it would be rather grating from the perspective of combining the old and new versions.  I could be convinced for the prefixed environment macro as there isn't as strong of a precedent there (there is only android which has an environment mapping macro that I can think of).

I'd worry that the non-prefixed versions would stomp on macros used by headers also provided elsewhere in the implementation (e.g., system headers).

Ah, but in those cases the headers are already non-conformant and breaking those, while unfortunate, is not entirely unreasonable.  IIRC, `_[A-Z]` is reserved for the implementation and `__` is left for the compiler.  (Yes, I am aware that many people simply follow the herd mentality and use header guards with `__` prefixes, but that is a separate discussion.)

Right, but the headers elsewhere in the implementation (such as libc or the kernel) are still part of the implementation.  I think you misread my previous comment.


I'm not sure I understand the concern with the prefixed versions.  For x86_64-apple-ios-simulator, I imagine we'd have:
```
__X86_64__ // or whatever the current one is
__APPLE__
__TARGET_ARCH_X86_64__
__TARGET_VENDOR_APPLE__
__TARGET_OS_IOS__
__TARGET_ENVIRONMENT_SIMULATOR__
```

I imagine legacy header code would use the old, un-prefixed macros, and new header code would use the prefixed macros.  It seems unlikely that much code would have logic depending on a mix of them.

Opinions?

One other option is to provide a function-like macros to accomplish this.  Similar to `__has_feature`, we could expose:

- `__is_auxiliary_os(os)`
- `__is_target_os(os)`
- `__is_auxiliary_environment(environment)`
- `__is_target_environment(environment)`
- `__is_auxiliary_vendor(vendor)`
- `__is_target_vendor(vendor)`

This would be cleaner overall I feel, but a much more radical departure for these cases.

FWIW, I like this idea quite a bit.

Although, maybe I just like it because I'm used to function calls.  It is a fairly radical departure.  It might be best to add a clean set of macros while we ponder what the right intrinsic design might be; we could always add them later.

So, Id like to point out that it is only as far a radical departure as clang is from gcc.  We already have `__has_feature`, `__has_extension`, `__is_identifier`, etc which already fit this model.  I believe that extending that model leaves plenty of prior art and will not be odd from a clang perspective, but only from the user perspective where they may be used to using `#if defined(__x86_64__)` rather than `#if __is_target_arch(x86_64)`.

Users would also need a `__has_feature(target_intrinsics)` check.

That would be a useful extension.  However, that would be beyond the scope of exposing the information in the triple.  The triple has 9 parts, and we already expose a number of them.  I think that exposing the critical pieces of arch, vendor, os, environment is a great start.  The other bits are something which we could expose in a follow up.

I’m not sure that I agree in this being so radical as to require significantly more effort than the traditional CPP macro approach.  I believe that getting the names correct is important, but we would face similar issues with the preprocessor approach.

BTW, you seem to be referencing an auxiliary target.  What's that about?

Keen eye :-).  It is to point out an additional dimension that would have been forgotten about otherwise.  CUDA compilation has a model where there is the primary target as well as a secondary (auxiliary) target which describes the host environment.


[arch][subarch][endian]-[vendor]-[os]-[environment][abi][object format][version]
 
diff --git lib/Frontend/InitPreprocessor.cpp lib/Frontend/InitPreprocessor.cpp
index 67c1faddc3..c1dfb39c37 100644
--- lib/Frontend/InitPreprocessor.cpp
+++ lib/Frontend/InitPreprocessor.cpp
@@ -713,6 +713,14 @@ static void InitializePredefinedMacros(const TargetInfo &TI,

   // Initialize target-specific preprocessor defines.

+  // Identify the OS and Environment fields of the target triple.
+  Builder.defineMacro("__TARGET_OS__",
+                      TI.getTriple().getOSTypeName(TI.getTriple().getOS()));
+  if (TI.getTriple().hasEnvironment()) {
+    Builder.defineMacro("__TARGET_ENVIRONMENT__",
+      TI.getTriple().getEnvironmentTypeName(TI.getTriple().getEnvironment()));
+  }
+
   // __BYTE_ORDER__ was added in GCC 4.6. It's analogous
   // to the macro __BYTE_ORDER (no trailing underscores)
   // from glibc's <endian.h> header.
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

-- 
Saleem Abdulrasool
compnerd (at) compnerd (dot) org




-- 
Saleem Abdulrasool
compnerd (at) compnerd (dot) org
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: new predefined macros to expose target triple values?

Robinson, Paul via cfe-dev
I posted a patch https://reviews.llvm.org/D41087 that implements Saleem’s idea (without auxiliary target support).
Cheers,
Alex

On Dec 9, 2017, at 4:18 PM, Saleem Abdulrasool <[hidden email]> wrote:



On Dec 9, 2017, at 3:04 PM, Duncan P. N. Exon Smith <[hidden email]> wrote:



On Dec 8, 2017, at 20:32, Saleem Abdulrasool <[hidden email]> wrote:



On Dec 6, 2017, at 12:10 PM, Duncan P. N. Exon Smith <[hidden email]> wrote:



On Dec 6, 2017, at 10:25, Duncan P. N. Exon Smith <[hidden email]> wrote:



On Dec 3, 2017, at 10:14, Saleem Abdulrasool via cfe-dev <[hidden email]> wrote:

On Fri, Dec 1, 2017 at 2:46 PM, Bob Wilson <[hidden email]> wrote:

On Dec 1, 2017, at 8:09 AM, Saleem Abdulrasool <[hidden email]> wrote:

On Thu, Nov 30, 2017 at 5:15 PM, Bob Wilson via cfe-dev <[hidden email]> wrote:
I wondering if anyone besides Apple would be interested in having predefined macros to identify the target OS and environment.

Background: Over the last few years, Apple has added several new platforms (tvOS and watchOS) as well as simulator variants for those, and we’ve accumulated a fair bit of complexity by adding Clang support for those platforms with a copy-and-paste approach. The -mmacosx-version-min option worked well when there was only one Apple platform, but it’s not so great now that we have a lot of -m*-version-min options. We’re trying to move toward the more standard approach of using the -target option to specify the target triple, including the OS version. Akira added support for that earlier (Clang r307982) and we’ve started moving toward identifying simulator targets via the environment field of the triple (LLVM r316380). Related to that, we also need a way to identify via predefined macros what target we’re building for. We recently added the __APPLE_EMBEDDED_SIMULATOR__ macro to distinguish the simulator targets (although it is currently not working when you use the -target option alone), but we don’t have anything to distinguish iOS vs. tvOS vs. watchOS.

It occurred to me that we can do this in a very general way by exposing the OS and Environment fields of the target triple directly in predefined macros. (We could do the same for the Arch and Vendor fields if anyone has a use for those.)

+1 on this.

Arch is exposed, although possibly with a different spelling (e.g. `__powerpc__`).  The vendor is also usually exposed in most toolchains (e.g. `__APPLE__`, or `__INTEL__` from ICC), but is not done in a generalized manner, and is something which I had intended to do as a cleanup.  If you can beat me to it, great!  The OS is already exposed (e.g. `__linux__`), but again suffers the problem of not being done in a generalized manner.
 
We could add these new macros only for Darwin targets, but since they are not at all Darwin-specific, what do you all think of adding them for all targets?

I think all the targets is pretty reasonable.  This actually came up a while ago, but we never went down this path for libc++.  Right now, libc++ is able to build with the itanium ABI and the Microsoft ABI on Windows.  Differentiating which ABI we are using is currently not possible, but access to the environment would solve the problem.

To be specific, here’s the change that I’m suggesting. (I’ll put it up for a proper review with a testcase if there is positive feedback for doing it this way.)

Most of the triple is made accessible, the environment is the one piece which is not, and I think it would be nice to have a uniform way of inspecting this information.

I believe that we could keep the existing architecture macros, uniformly expose vendor as `__<upper case vendor>__`.  The OS could be exposed uniformly as `__<lower case os>__`.  The environment, since there is no real precedent, we can bike shed (fuchsia seems to be a popular color these days :-p), but no strong opinions on that.  I believe if we expose the environment, all 9 pieces of the triple should be visible.

This is different than what I was proposing here, but you made me realize the fatal flaw in my proposal: there isn’t a good way to do string comparisons in preprocessor conditionals. My thought was that we could write things like:

#if __TARGET_ENVIRONMENT__ == simulator

for code that is specific to the iOS simulator (for example), but of course that doesn’t work.

Yeah, its unfortunate that that pattern isn't really feasible.
 
Alternatively, we could adopt a convention of prefixing the macro names with the triple component name, e.g.:

__TARGET_ENVIRONMENT_SIMULATOR__
__TARGET_OS_TVOS__
etc.

instead of the raw values:

__SIMULATOR__
__TVOS__

It would make sense to me to add the __TARGET-prefixed versions for all targets, but if we do the raw values I would be inclined to do them selectively for platforms that I understand and can test. I slightly prefer the prefixed versions — they give context to show that the macros are about the environment or OS. On the other hand, I suppose they would be a departure from the existing convention with macros like __APPLE__ and __ELF__.

I actually prefer the un-prefixed versions.  I can appreciate the additional context, but, it would be rather grating from the perspective of combining the old and new versions.  I could be convinced for the prefixed environment macro as there isn't as strong of a precedent there (there is only android which has an environment mapping macro that I can think of).

I'd worry that the non-prefixed versions would stomp on macros used by headers also provided elsewhere in the implementation (e.g., system headers).

Ah, but in those cases the headers are already non-conformant and breaking those, while unfortunate, is not entirely unreasonable.  IIRC, `_[A-Z]` is reserved for the implementation and `__` is left for the compiler.  (Yes, I am aware that many people simply follow the herd mentality and use header guards with `__` prefixes, but that is a separate discussion.)

Right, but the headers elsewhere in the implementation (such as libc or the kernel) are still part of the implementation.  I think you misread my previous comment.


I'm not sure I understand the concern with the prefixed versions.  For x86_64-apple-ios-simulator, I imagine we'd have:
```
__X86_64__ // or whatever the current one is
__APPLE__
__TARGET_ARCH_X86_64__
__TARGET_VENDOR_APPLE__
__TARGET_OS_IOS__
__TARGET_ENVIRONMENT_SIMULATOR__
```

I imagine legacy header code would use the old, un-prefixed macros, and new header code would use the prefixed macros.  It seems unlikely that much code would have logic depending on a mix of them.

Opinions?

One other option is to provide a function-like macros to accomplish this.  Similar to `__has_feature`, we could expose:

- `__is_auxiliary_os(os)`
- `__is_target_os(os)`
- `__is_auxiliary_environment(environment)`
- `__is_target_environment(environment)`
- `__is_auxiliary_vendor(vendor)`
- `__is_target_vendor(vendor)`

This would be cleaner overall I feel, but a much more radical departure for these cases.

FWIW, I like this idea quite a bit.

Although, maybe I just like it because I'm used to function calls.  It is a fairly radical departure.  It might be best to add a clean set of macros while we ponder what the right intrinsic design might be; we could always add them later.

So, Id like to point out that it is only as far a radical departure as clang is from gcc.  We already have `__has_feature`, `__has_extension`, `__is_identifier`, etc which already fit this model.  I believe that extending that model leaves plenty of prior art and will not be odd from a clang perspective, but only from the user perspective where they may be used to using `#if defined(__x86_64__)` rather than `#if __is_target_arch(x86_64)`.

Users would also need a `__has_feature(target_intrinsics)` check.

That would be a useful extension.  However, that would be beyond the scope of exposing the information in the triple.  The triple has 9 parts, and we already expose a number of them.  I think that exposing the critical pieces of arch, vendor, os, environment is a great start.  The other bits are something which we could expose in a follow up.

I’m not sure that I agree in this being so radical as to require significantly more effort than the traditional CPP macro approach.  I believe that getting the names correct is important, but we would face similar issues with the preprocessor approach.

BTW, you seem to be referencing an auxiliary target.  What's that about?

Keen eye :-).  It is to point out an additional dimension that would have been forgotten about otherwise.  CUDA compilation has a model where there is the primary target as well as a secondary (auxiliary) target which describes the host environment.


[arch][subarch][endian]-[vendor]-[os]-[environment][abi][object format][version]
 
diff --git lib/Frontend/InitPreprocessor.cpp lib/Frontend/InitPreprocessor.cpp
index 67c1faddc3..c1dfb39c37 100644
--- lib/Frontend/InitPreprocessor.cpp
+++ lib/Frontend/InitPreprocessor.cpp
@@ -713,6 +713,14 @@ static void InitializePredefinedMacros(const TargetInfo &TI,

   // Initialize target-specific preprocessor defines.

+  // Identify the OS and Environment fields of the target triple.
+  Builder.defineMacro("__TARGET_OS__",
+                      TI.getTriple().getOSTypeName(TI.getTriple().getOS()));
+  if (TI.getTriple().hasEnvironment()) {
+    Builder.defineMacro("__TARGET_ENVIRONMENT__",
+      TI.getTriple().getEnvironmentTypeName(TI.getTriple().getEnvironment()));
+  }
+
   // __BYTE_ORDER__ was added in GCC 4.6. It's analogous
   // to the macro __BYTE_ORDER (no trailing underscores)
   // from glibc's <endian.h> header.
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

-- 
Saleem Abdulrasool
compnerd (at) compnerd (dot) org




-- 
Saleem Abdulrasool
compnerd (at) compnerd (dot) org
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev