[PATCH] D137995: [Flang][Driver] Handle target CPU and features

2022-12-02 Thread Usman Nadeem via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG3951a73490df: [Flang][Driver] Handle target CPU and features 
(authored by mnadeem).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D137995/new/

https://reviews.llvm.org/D137995

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Driver/ToolChains/CommonArgs.cpp
  clang/lib/Driver/ToolChains/CommonArgs.h
  clang/lib/Driver/ToolChains/Flang.cpp
  clang/lib/Driver/ToolChains/Flang.h
  flang/include/flang/Frontend/TargetOptions.h
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/lib/Frontend/FrontendActions.cpp
  flang/test/Driver/driver-help.f90
  flang/test/Driver/target-cpu-features.f90

Index: flang/test/Driver/target-cpu-features.f90
===
--- /dev/null
+++ flang/test/Driver/target-cpu-features.f90
@@ -0,0 +1,56 @@
+! REQUIRES: aarch64-registered-target, x86-registered-target
+
+! Test that -mcpu/march are used and that the -target-cpu and -target-features
+! are also added to the fc1 command.
+
+! RUN: %flang --target=aarch64-linux-gnu -mcpu=cortex-a57 -c %s -### 2>&1 \
+! RUN: | FileCheck %s -check-prefix=CHECK-A57
+
+! RUN: %flang --target=aarch64-linux-gnu -mcpu=cortex-a76 -c %s -### 2>&1 \
+! RUN: | FileCheck %s -check-prefix=CHECK-A76
+
+! RUN: %flang --target=aarch64-linux-gnu -march=armv9 -c %s -### 2>&1 \
+! RUN: | FileCheck %s -check-prefix=CHECK-ARMV9
+
+! Negative test. ARM cpu with x86 target.
+! RUN: %flang --target=x86_64-linux-gnu -mcpu=cortex-a57 -c %s -### 2>&1 \
+! RUN: | FileCheck %s -check-prefix=CHECK-NO-A57
+
+! RUN: %flang --target=x86_64-linux-gnu -march=skylake -c %s -### 2>&1 \
+! RUN: | FileCheck %s -check-prefix=CHECK-SKYLAKE
+
+! RUN: %flang --target=x86_64h-linux-gnu -c %s -### 2>&1 \
+! RUN: | FileCheck %s -check-prefix=CHECK-X86_64H
+
+
+! Test that invalid cpu and features are ignored.
+
+! RUN: %flang_fc1 -triple aarch64-linux-gnu -target-cpu supercpu \
+! RUN: -o /dev/null -S %s 2>&1 | FileCheck %s -check-prefix=CHECK-INVALID-CPU
+
+! RUN: %flang_fc1 -triple aarch64-linux-gnu -target-feature +superspeed \
+! RUN: -o /dev/null -S %s 2>&1 | FileCheck %s -check-prefix=CHECK-INVALID-FEATURE
+
+
+! CHECK-A57: "-fc1" "-triple" "aarch64-unknown-linux-gnu"
+! CHECK-A57-SAME: "-target-cpu" "cortex-a57" "-target-feature" "+v8a" "-target-feature" "+crc" "-target-feature" "+crypto" "-target-feature" "+fp-armv8" "-target-feature" "+neon" "-target-feature" "+sha2" "-target-feature" "+aes"
+
+! CHECK-A76: "-fc1" "-triple" "aarch64-unknown-linux-gnu"
+! CHECK-A76-SAME: "-target-cpu" "cortex-a76" "-target-feature" "+v8.2a" "-target-feature" "+crc" "-target-feature" "+lse" "-target-feature" "+rdm" "-target-feature" "+crypto" "-target-feature" "+dotprod" "-target-feature" "+fp-armv8" "-target-feature" "+neon" "-target-feature" "+fullfp16" "-target-feature" "+ras" "-target-feature" "+rcpc" "-target-feature" "+ssbs" "-target-feature" "+sha2" "-target-feature" "+aes"
+
+! CHECK-ARMV9: "-fc1" "-triple" "aarch64-unknown-linux-gnu"
+! CHECK-ARMV9-SAME: "-target-cpu" "generic" "-target-feature" "+neon" "-target-feature" "+v9a" "-target-feature" "+sve" "-target-feature" "+sve2"
+
+! CHECK-NO-A57: "-fc1" "-triple" "x86_64-unknown-linux-gnu"
+! CHECK-NO-A57-NOT: cortex-a57
+! CHECK-NO-A57-SAME: "-target-cpu" "x86-64"
+! CHECK-NO-A57-NOT: cortex-a57
+
+! CHECK-SKYLAKE: "-fc1" "-triple" "x86_64-unknown-linux-gnu"
+! CHECK-SKYLAKE-SAME: "-target-cpu" "skylake"
+
+! CHECK-X86_64H: "-fc1" "-triple" "x86_64h-unknown-linux-gnu"
+! CHECK-X86_64H-SAME: "-target-cpu" "x86-64" "-target-feature" "-rdrnd" "-target-feature" "-aes" "-target-feature" "-pclmul" "-target-feature" "-rtm" "-target-feature" "-fsgsbase"
+
+! CHECK-INVALID-CPU: 'supercpu' is not a recognized processor for this target (ignoring processor)
+! CHECK-INVALID-FEATURE: '+superspeed' is not a recognized feature for this target (ignoring feature)
Index: flang/test/Driver/driver-help.f90
===
--- flang/test/Driver/driver-help.f90
+++ flang/test/Driver/driver-help.f90
@@ -156,6 +156,8 @@
 ! HELP-FC1-NEXT: -P Disable linemarker output in -E mode
 ! HELP-FC1-NEXT: -std=   Language standard to compile for
 ! HELP-FC1-NEXT: -S Only run preprocess and compilation steps
+! HELP-FC1-NEXT: -target-cpu Target a specific cpu type
+! HELP-FC1-NEXT: -target-feature  Target specific attributes
 ! HELP-FC1-NEXT: -test-io   Run the InputOuputTest action. Use for development and testing only.
 ! HELP-FC1-NEXT: -triple Specify target triple (e.g. i686-apple-darwin9)
 ! HELP-FC1-NEXT: -U  Undefine macro 
Index: flang/lib/Frontend/FrontendActions.cpp
===
--- flang/lib/Frontend/FrontendActions.cpp
+++ 

[PATCH] D137995: [Flang][Driver] Handle target CPU and features

2022-12-02 Thread Andrzej Warzynski via Phabricator via cfe-commits
awarzynski accepted this revision.
awarzynski added a comment.
This revision is now accepted and ready to land.

LGTM, thank you!


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D137995/new/

https://reviews.llvm.org/D137995

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D137995: [Flang][Driver] Handle target CPU and features

2022-12-01 Thread Usman Nadeem via Phabricator via cfe-commits
mnadeem updated this revision to Diff 479474.
mnadeem marked 4 inline comments as done.
mnadeem added a comment.

- Address comments.
- Add fc1 tests.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D137995/new/

https://reviews.llvm.org/D137995

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Driver/ToolChains/CommonArgs.cpp
  clang/lib/Driver/ToolChains/CommonArgs.h
  clang/lib/Driver/ToolChains/Flang.cpp
  clang/lib/Driver/ToolChains/Flang.h
  flang/include/flang/Frontend/TargetOptions.h
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/lib/Frontend/FrontendActions.cpp
  flang/test/Driver/driver-help.f90
  flang/test/Driver/target-cpu-features.f90

Index: flang/test/Driver/target-cpu-features.f90
===
--- /dev/null
+++ flang/test/Driver/target-cpu-features.f90
@@ -0,0 +1,56 @@
+! REQUIRES: aarch64-registered-target, x86-registered-target
+
+! Test that -mcpu/march are used and that the -target-cpu and -target-features
+! are also added to the fc1 command.
+
+! RUN: %flang --target=aarch64-linux-gnu -mcpu=cortex-a57 -c %s -### 2>&1 \
+! RUN: | FileCheck %s -check-prefix=CHECK-A57
+
+! RUN: %flang --target=aarch64-linux-gnu -mcpu=cortex-a76 -c %s -### 2>&1 \
+! RUN: | FileCheck %s -check-prefix=CHECK-A76
+
+! RUN: %flang --target=aarch64-linux-gnu -march=armv9 -c %s -### 2>&1 \
+! RUN: | FileCheck %s -check-prefix=CHECK-ARMV9
+
+! Negative test. ARM cpu with x86 target.
+! RUN: %flang --target=x86_64-linux-gnu -mcpu=cortex-a57 -c %s -### 2>&1 \
+! RUN: | FileCheck %s -check-prefix=CHECK-NO-A57
+
+! RUN: %flang --target=x86_64-linux-gnu -march=skylake -c %s -### 2>&1 \
+! RUN: | FileCheck %s -check-prefix=CHECK-SKYLAKE
+
+! RUN: %flang --target=x86_64h-linux-gnu -c %s -### 2>&1 \
+! RUN: | FileCheck %s -check-prefix=CHECK-X86_64H
+
+
+! Test that invalid cpu and features are ignored.
+
+! RUN: %flang_fc1 -triple aarch64-linux-gnu -target-cpu supercpu \
+! RUN: -o /dev/null -S %s 2>&1 | FileCheck %s -check-prefix=CHECK-INVALID-CPU
+
+! RUN: %flang_fc1 -triple aarch64-linux-gnu -target-feature +superspeed \
+! RUN: -o /dev/null -S %s 2>&1 | FileCheck %s -check-prefix=CHECK-INVALID-FEATURE
+
+
+! CHECK-A57: "-fc1" "-triple" "aarch64-unknown-linux-gnu"
+! CHECK-A57-SAME: "-target-cpu" "cortex-a57" "-target-feature" "+v8a" "-target-feature" "+crc" "-target-feature" "+crypto" "-target-feature" "+fp-armv8" "-target-feature" "+neon" "-target-feature" "+sha2" "-target-feature" "+aes"
+
+! CHECK-A76: "-fc1" "-triple" "aarch64-unknown-linux-gnu"
+! CHECK-A76-SAME: "-target-cpu" "cortex-a76" "-target-feature" "+v8.2a" "-target-feature" "+crc" "-target-feature" "+lse" "-target-feature" "+rdm" "-target-feature" "+crypto" "-target-feature" "+dotprod" "-target-feature" "+fp-armv8" "-target-feature" "+neon" "-target-feature" "+fullfp16" "-target-feature" "+ras" "-target-feature" "+rcpc" "-target-feature" "+ssbs" "-target-feature" "+sha2" "-target-feature" "+aes"
+
+! CHECK-ARMV9: "-fc1" "-triple" "aarch64-unknown-linux-gnu"
+! CHECK-ARMV9-SAME: "-target-cpu" "generic" "-target-feature" "+neon" "-target-feature" "+v9a" "-target-feature" "+sve" "-target-feature" "+sve2"
+
+! CHECK-NO-A57: "-fc1" "-triple" "x86_64-unknown-linux-gnu"
+! CHECK-NO-A57-NOT: cortex-a57
+! CHECK-NO-A57-SAME: "-target-cpu" "x86-64"
+! CHECK-NO-A57-NOT: cortex-a57
+
+! CHECK-SKYLAKE: "-fc1" "-triple" "x86_64-unknown-linux-gnu"
+! CHECK-SKYLAKE-SAME: "-target-cpu" "skylake"
+
+! CHECK-X86_64H: "-fc1" "-triple" "x86_64h-unknown-linux-gnu"
+! CHECK-X86_64H-SAME: "-target-cpu" "x86-64" "-target-feature" "-rdrnd" "-target-feature" "-aes" "-target-feature" "-pclmul" "-target-feature" "-rtm" "-target-feature" "-fsgsbase"
+
+! CHECK-INVALID-CPU: 'supercpu' is not a recognized processor for this target (ignoring processor)
+! CHECK-INVALID-FEATURE: '+superspeed' is not a recognized feature for this target (ignoring feature)
Index: flang/test/Driver/driver-help.f90
===
--- flang/test/Driver/driver-help.f90
+++ flang/test/Driver/driver-help.f90
@@ -156,6 +156,8 @@
 ! HELP-FC1-NEXT: -P Disable linemarker output in -E mode
 ! HELP-FC1-NEXT: -std=   Language standard to compile for
 ! HELP-FC1-NEXT: -S Only run preprocess and compilation steps
+! HELP-FC1-NEXT: -target-cpu Target a specific cpu type
+! HELP-FC1-NEXT: -target-feature  Target specific attributes
 ! HELP-FC1-NEXT: -test-io   Run the InputOuputTest action. Use for development and testing only.
 ! HELP-FC1-NEXT: -triple Specify target triple (e.g. i686-apple-darwin9)
 ! HELP-FC1-NEXT: -U  Undefine macro 
Index: flang/lib/Frontend/FrontendActions.cpp
===
--- flang/lib/Frontend/FrontendActions.cpp
+++ flang/lib/Frontend/FrontendActions.cpp
@@ -591,7 +591,8 @@
 void 

[PATCH] D137995: [Flang][Driver] Handle target CPU and features

2022-11-30 Thread Andrzej Warzynski via Phabricator via cfe-commits
awarzynski added a comment.

Thanks for all the updates @mnadeem! Mostly looks good. A few more nits, but 
nothing substantial :)

In D137995#3958824 , @mnadeem wrote:

> In D137995#3931005 , 
> @kiranchandramohan wrote:
>
>> We might need `-fc1` tests as well.
>
> What kind of tests do you think would be appropriate here? Can you point me 
> to any examples, maybe from clang?

I didn't see any tests in Clang specifically for the frontend driver flags taht 
are added here (`i.e. `-target-cpu` and `-target-feature`). However, you could 
add a test for situations like this: `-target-cpu=my-imaginary-cpu` and 
`-target-fature=my-amazing-non-existent-feature`.




Comment at: clang/lib/Driver/ToolChains/Flang.cpp:98
+  default:
+// Untested for other targets but should work generally.
+break;

[nit] I don't think that this comment contributes much. To me it is 
self-explanatory that only the triples that are actually present here are 
tested. Having said that, I don't mind keeping it here.



Comment at: clang/lib/Driver/ToolChains/Flang.cpp:100
+break;
+  case llvm::Triple::aarch64:
+  case llvm::Triple::x86_64:

nit



Comment at: flang/include/flang/Frontend/TargetOptions.h:25-30
 /// Options for controlling the target. Currently this is just a placeholder.
 /// In the future, we will use this to specify various target options that
 /// will affect the generated code e.g.:
 ///   * CPU to tune the code for
-///   * available CPU/hardware extensions
-///   * target specific features to enable/disable
 ///   * options for accelerators (e.g. GPUs)
 ///   * (...)

I think that we can trim this now. WDYT?



Comment at: flang/test/Driver/target-cpu-features.f90:25-26
+
+! CHECK-A57: "-triple" "aarch64-unknown-linux-gnu"
+! CHECK-A57: "-target-cpu" "cortex-a57" "-target-feature" "+v8a" 
"-target-feature" "+crc" "-target-feature" "+crypto" "-target-feature" 
"+fp-armv8" "-target-feature" "+neon" "-target-feature" "+sha2" 
"-target-feature" "+aes"
+

1. By adding `-fc1` you make it clear that it's the frontend driver invocation 
that's being tested.
2. `CHECK-SAME` makes sure the triple is matched with the right set of features.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D137995/new/

https://reviews.llvm.org/D137995

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D137995: [Flang][Driver] Handle target CPU and features

2022-11-29 Thread Usman Nadeem via Phabricator via cfe-commits
mnadeem added inline comments.



Comment at: clang/lib/Driver/ToolChains/Flang.cpp:99
+  default:
+// Untested for other targets but should work generally.
+break;

mnadeem wrote:
> kiranchandramohan wrote:
> > I get a segfault in ` 
> > Fortran::frontend::CodeGenAction::setUpTargetMachine()` currently when 
> > using `./bin/flang-new --target=x86_64-linux-gnu  file.f90`
> I am unable to reproduce this, do you have your toolchain built for x86 
> target as well?
It might be unrelated to this patch.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D137995/new/

https://reviews.llvm.org/D137995

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D137995: [Flang][Driver] Handle target CPU and features

2022-11-29 Thread Usman Nadeem via Phabricator via cfe-commits
mnadeem added inline comments.



Comment at: clang/lib/Driver/ToolChains/Flang.cpp:99
+  default:
+// Untested for other targets but should work generally.
+break;

kiranchandramohan wrote:
> I get a segfault in ` Fortran::frontend::CodeGenAction::setUpTargetMachine()` 
> currently when using `./bin/flang-new --target=x86_64-linux-gnu  file.f90`
I am unable to reproduce this, do you have your toolchain built for x86 target 
as well?


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D137995/new/

https://reviews.llvm.org/D137995

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D137995: [Flang][Driver] Handle target CPU and features

2022-11-29 Thread Usman Nadeem via Phabricator via cfe-commits
mnadeem added a comment.

In D137995#3931005 , 
@kiranchandramohan wrote:

> We might need `-fc1` tests as well.

What kind of tests do you think would be appropriate here? Can you point me to 
any examples, maybe from clang?


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D137995/new/

https://reviews.llvm.org/D137995

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D137995: [Flang][Driver] Handle target CPU and features

2022-11-29 Thread Usman Nadeem via Phabricator via cfe-commits
mnadeem added a comment.

In D137995#3944145 , @awarzynski 
wrote:

> Thanks for implementing this!
>
>> Processes target cpu and features in the flang driver. Right now features 
>> are only added for AArch64 because I only did basic testing on AArch64 but 
>> it should generally work for others as well.
>
> X86 is a very popular target and we have pre-commit CI as well. And X86 
> buildbots :) Please include X86.
>
> Question: are the option semantics identical that what you get in `clang 
> -cc1`? If yes, could you add a comment in the summary?

Included x86 and added a comment in the summary.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D137995/new/

https://reviews.llvm.org/D137995

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D137995: [Flang][Driver] Handle target CPU and features

2022-11-29 Thread Usman Nadeem via Phabricator via cfe-commits
mnadeem updated this revision to Diff 478766.
mnadeem marked 4 inline comments as done.
mnadeem edited the summary of this revision.
Herald added a subscriber: pengfei.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D137995/new/

https://reviews.llvm.org/D137995

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Driver/ToolChains/CommonArgs.cpp
  clang/lib/Driver/ToolChains/CommonArgs.h
  clang/lib/Driver/ToolChains/Flang.cpp
  clang/lib/Driver/ToolChains/Flang.h
  flang/include/flang/Frontend/TargetOptions.h
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/lib/Frontend/FrontendActions.cpp
  flang/test/Driver/driver-help.f90
  flang/test/Driver/target-cpu-features.f90

Index: flang/test/Driver/target-cpu-features.f90
===
--- /dev/null
+++ flang/test/Driver/target-cpu-features.f90
@@ -0,0 +1,46 @@
+! REQUIRES: aarch64-registered-target, x86-registered-target
+
+! Test that -mcpu/march are used and that the -target-cpu and target features
+! are also added.
+
+! RUN: %flang --target=aarch64-linux-gnu -mcpu=cortex-a57 -c %s -### 2>&1 \
+! RUN: | FileCheck %s -check-prefix=CHECK-A57
+
+! RUN: %flang --target=aarch64-linux-gnu -mcpu=cortex-a76 -c %s -### 2>&1 \
+! RUN: | FileCheck %s -check-prefix=CHECK-A76
+
+! RUN: %flang --target=aarch64-linux-gnu -march=armv9 -c %s -### 2>&1 \
+! RUN: | FileCheck %s -check-prefix=CHECK-ARMV9
+
+! Negative test. ARM cpu with x86 target.
+! RUN: %flang --target=x86_64-linux-gnu -mcpu=cortex-a57 -c %s -### 2>&1 \
+! RUN: | FileCheck %s -check-prefix=CHECK-NO-A57
+
+! RUN: %flang --target=x86_64-linux-gnu -march=skylake -c %s -### 2>&1 \
+! RUN: | FileCheck %s -check-prefix=CHECK-SKYLAKE
+
+! RUN: %flang --target=x86_64h-linux-gnu -c %s -### 2>&1 \
+! RUN: | FileCheck %s -check-prefix=CHECK-X86_64H
+
+! CHECK-A57: "-triple" "aarch64-unknown-linux-gnu"
+! CHECK-A57: "-target-cpu" "cortex-a57" "-target-feature" "+v8a" "-target-feature" "+crc" "-target-feature" "+crypto" "-target-feature" "+fp-armv8" "-target-feature" "+neon" "-target-feature" "+sha2" "-target-feature" "+aes"
+
+! CHECK-A76: "-triple" "aarch64-unknown-linux-gnu"
+! CHECK-A76: "-target-cpu" "cortex-a76" "-target-feature" "+v8.2a" "-target-feature" "+crc" "-target-feature" "+lse" "-target-feature" "+rdm" "-target-feature" "+crypto" "-target-feature" "+dotprod" "-target-feature" "+fp-armv8" "-target-feature" "+neon" "-target-feature" "+fullfp16" "-target-feature" "+ras" "-target-feature" "+rcpc" "-target-feature" "+ssbs" "-target-feature" "+sha2" "-target-feature" "+aes"
+
+! CHECK-ARMV9: "-triple" "aarch64-unknown-linux-gnu"
+! CHECK-ARMV9: "-target-cpu" "generic" "-target-feature" "+neon" "-target-feature" "+v9a" "-target-feature" "+sve" "-target-feature" "+sve2"
+
+! CHECK-NO-A57: "-fc1"
+! CHECK-NO-A57: "-triple" "x86_64-unknown-linux-gnu"
+! CHECK-NO-A57-NOT: cortex-a57
+! CHECK-NO-A57: "-target-cpu" "x86-64"
+! CHECK-NO-A57-NOT: cortex-a57
+
+! CHECK-SKYLAKE: "-fc1"
+! CHECK-SKYLAKE: "-triple" "x86_64-unknown-linux-gnu"
+! CHECK-SKYLAKE: "-target-cpu" "skylake"
+
+! CHECK-X86_64H: "-fc1"
+! CHECK-X86_64H: "-triple" "x86_64h-unknown-linux-gnu"
+! CHECK-X86_64H: "-target-cpu" "x86-64" "-target-feature" "-rdrnd" "-target-feature" "-aes" "-target-feature" "-pclmul" "-target-feature" "-rtm" "-target-feature" "-fsgsbase"
Index: flang/test/Driver/driver-help.f90
===
--- flang/test/Driver/driver-help.f90
+++ flang/test/Driver/driver-help.f90
@@ -156,6 +156,8 @@
 ! HELP-FC1-NEXT: -P Disable linemarker output in -E mode
 ! HELP-FC1-NEXT: -std=   Language standard to compile for
 ! HELP-FC1-NEXT: -S Only run preprocess and compilation steps
+! HELP-FC1-NEXT: -target-cpu Target a specific cpu type
+! HELP-FC1-NEXT: -target-feature  Target specific attributes
 ! HELP-FC1-NEXT: -test-io   Run the InputOuputTest action. Use for development and testing only.
 ! HELP-FC1-NEXT: -triple Specify target triple (e.g. i686-apple-darwin9)
 ! HELP-FC1-NEXT: -U  Undefine macro 
Index: flang/lib/Frontend/FrontendActions.cpp
===
--- flang/lib/Frontend/FrontendActions.cpp
+++ flang/lib/Frontend/FrontendActions.cpp
@@ -591,7 +591,8 @@
 void CodeGenAction::setUpTargetMachine() {
   CompilerInstance  = this->getInstance();
 
-  const std::string  = ci.getInvocation().getTargetOpts().triple;
+  const TargetOptions  = ci.getInvocation().getTargetOpts();
+  const std::string  = targetOpts.triple;
 
   // Create `Target`
   std::string error;
@@ -602,9 +603,11 @@
   // Create `TargetMachine`
   const auto  = ci.getInvocation().getCodeGenOpts();
   llvm::CodeGenOpt::Level OptLevel = getCGOptLevel(CGOpts);
+  std::string featuresStr = llvm::join(targetOpts.featuresAsWritten.begin(),
+

[PATCH] D137995: [Flang][Driver] Handle target CPU and features

2022-11-22 Thread Andrzej Warzynski via Phabricator via cfe-commits
awarzynski added a comment.

Thanks for implementing this!

> Processes target cpu and features in the flang driver. Right now features are 
> only added for AArch64 because I only did basic testing on AArch64 but it 
> should generally work for others as well.

X86 is a very popular target and we have pre-commit CI as well. And X86 
buildbots :) Please include X86.

Question: are the option semantics identical that what you get in `clang -cc1`? 
If yes, could you add a comment in the summary?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D137995/new/

https://reviews.llvm.org/D137995

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D137995: [Flang][Driver] Handle target CPU and features

2022-11-16 Thread Kiran Chandramohan via Phabricator via cfe-commits
kiranchandramohan added a comment.

Thanks @mnadeem for this patch. A few minor comments first. Try to replace auto 
in all places except where the type is on the RHS.

We might need `-fc1` tests as well.




Comment at: clang/lib/Driver/ToolChains/Flang.cpp:85
+ ArgStringList ) const {
+  // return;
+  const auto  = getToolChain();

Nit: leftover code?



Comment at: clang/lib/Driver/ToolChains/Flang.cpp:86
+  // return;
+  const auto  = getToolChain();
+  const llvm::Triple  = TC.getEffectiveTriple();

Nit: replace auto.



Comment at: clang/lib/Driver/ToolChains/Flang.cpp:99
+  default:
+// Untested for other targets but should work generally.
+break;

I get a segfault in ` Fortran::frontend::CodeGenAction::setUpTargetMachine()` 
currently when using `./bin/flang-new --target=x86_64-linux-gnu  file.f90`



Comment at: flang/lib/Frontend/CompilerInvocation.cpp:179
+
+  for (const auto *currentArg :
+   args.filtered(clang::driver::options::OPT_target_feature))

Nit: Can you remove the auto here?



Comment at: flang/lib/Frontend/FrontendActions.cpp:594
 
-  const std::string  = ci.getInvocation().getTargetOpts().triple;
+  const auto  = ci.getInvocation().getTargetOpts();
+  const std::string  = targetOpts.triple;

Nit: Can you remove the auto here?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D137995/new/

https://reviews.llvm.org/D137995

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D137995: [Flang][Driver] Handle target CPU and features

2022-11-14 Thread Usman Nadeem via Phabricator via cfe-commits
mnadeem created this revision.
mnadeem added reviewers: vzakhari, awarzynski, kiranchandramohan.
mnadeem added a project: Flang.
Herald added subscribers: ctetreau, jdoerfert, s.egerton, simoncook, 
fedor.sergeev, kristof.beyls, dschuff.
Herald added a reviewer: sscalpone.
Herald added a project: All.
mnadeem requested review of this revision.
Herald added subscribers: cfe-commits, pcwang-thead, MaskRay, aheejin.
Herald added a project: clang.

This patch:

- Adds `target-feature` and `target-cpu` to FC1Options.
- Moves `getTargetFeatures()` from Clang.cpp to CommonArgs.cpp.
- Processes target cpu and features in the flang driver. Right now features are 
only added for AArch64 because I only did basic testing on AArch64 but it 
should generally work for others as well.
- Adds appropriate structures in TargetOptions and passes them to the target 
machine.

What's missing:

- Adding the CPU info and the features as attributes in the LLVM IR module.
- Processing target specific flags, e.g. SVE vector bits for AArch64, ABI etc.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D137995

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Driver/ToolChains/CommonArgs.cpp
  clang/lib/Driver/ToolChains/CommonArgs.h
  clang/lib/Driver/ToolChains/Flang.cpp
  clang/lib/Driver/ToolChains/Flang.h
  flang/include/flang/Frontend/TargetOptions.h
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/lib/Frontend/FrontendActions.cpp
  flang/test/Driver/driver-help.f90
  flang/test/Driver/target-cpu-features.f90

Index: flang/test/Driver/target-cpu-features.f90
===
--- /dev/null
+++ flang/test/Driver/target-cpu-features.f90
@@ -0,0 +1,22 @@
+! REQUIRES: aarch64-registered-target, x86-registered-target
+
+! Test that -mcpu is forwarded to -target-cpu and that the target features are
+! also added.
+
+! RUN: %flang --target=aarch64-linux-gnu -mcpu=cortex-a57 -emit-llvm -c %s \
+! RUN:   -o %t.o -### 2>&1 | FileCheck %s -check-prefix=CHECK-A57
+
+! RUN: %flang --target=aarch64-linux-gnu -mcpu=cortex-a76 -emit-llvm -c %s \
+! RUN:   -o %t.o -### 2>&1 | FileCheck %s -check-prefix=CHECK-A76
+
+! RUN: %flang --target=x86_64-linux-gnu -mcpu=cortex-a57 -emit-llvm -c %s \
+! RUN:   -o %t.o -### 2>&1 | FileCheck %s -check-prefix=CHECK-NO-A57
+
+! CHECK-A57: "-triple" "aarch64-unknown-linux-gnu"
+! CHECK-A57: "-target-cpu" "cortex-a57" "-target-feature" "+v8a" "-target-feature" "+crc" "-target-feature" "+crypto" "-target-feature" "+fp-armv8" "-target-feature" "+neon" "-target-feature" "+sha2" "-target-feature" "+aes"
+
+! CHECK-A76: "-triple" "aarch64-unknown-linux-gnu"
+! CHECK-A76: "-target-cpu" "cortex-a76" "-target-feature" "+v8.2a" "-target-feature" "+crc" "-target-feature" "+lse" "-target-feature" "+rdm" "-target-feature" "+crypto" "-target-feature" "+dotprod" "-target-feature" "+fp-armv8" "-target-feature" "+neon" "-target-feature" "+fullfp16" "-target-feature" "+ras" "-target-feature" "+rcpc" "-target-feature" "+ssbs" "-target-feature" "+sha2" "-target-feature" "+aes"
+
+! CHECK-NO-A57: "-fc1"
+! CHECK-NO-A57-NOT: cortex-a57
Index: flang/test/Driver/driver-help.f90
===
--- flang/test/Driver/driver-help.f90
+++ flang/test/Driver/driver-help.f90
@@ -156,6 +156,8 @@
 ! HELP-FC1-NEXT: -P Disable linemarker output in -E mode
 ! HELP-FC1-NEXT: -std=   Language standard to compile for
 ! HELP-FC1-NEXT: -S Only run preprocess and compilation steps
+! HELP-FC1-NEXT: -target-cpu Target a specific cpu type
+! HELP-FC1-NEXT: -target-feature  Target specific attributes
 ! HELP-FC1-NEXT: -test-io   Run the InputOuputTest action. Use for development and testing only.
 ! HELP-FC1-NEXT: -triple Specify target triple (e.g. i686-apple-darwin9)
 ! HELP-FC1-NEXT: -U  Undefine macro 
Index: flang/lib/Frontend/FrontendActions.cpp
===
--- flang/lib/Frontend/FrontendActions.cpp
+++ flang/lib/Frontend/FrontendActions.cpp
@@ -591,7 +591,8 @@
 void CodeGenAction::setUpTargetMachine() {
   CompilerInstance  = this->getInstance();
 
-  const std::string  = ci.getInvocation().getTargetOpts().triple;
+  const auto  = ci.getInvocation().getTargetOpts();
+  const std::string  = targetOpts.triple;
 
   // Create `Target`
   std::string error;
@@ -602,9 +603,11 @@
   // Create `TargetMachine`
   const auto  = ci.getInvocation().getCodeGenOpts();
   llvm::CodeGenOpt::Level OptLevel = getCGOptLevel(CGOpts);
+  std::string featuresStr = llvm::join(targetOpts.featuresAsWritten.begin(),
+   targetOpts.featuresAsWritten.end(), ",");
   tm.reset(theTarget->createTargetMachine(
-  theTriple, /*CPU=*/"",
-  /*Features=*/"", llvm::TargetOptions(),
+  theTriple, /*CPU=*/targetOpts.cpu,
+