[clang] ac892c7 - [OMPIRBuilder] Add support for simdlen clause

2022-07-11 Thread Arnamoy Bhattacharyya via cfe-commits

Author: Prabhdeep Singh Soni
Date: 2022-07-11T13:29:06-04:00
New Revision: ac892c70a456a443ef5d52ec886b4b6b0fae9244

URL: 
https://github.com/llvm/llvm-project/commit/ac892c70a456a443ef5d52ec886b4b6b0fae9244
DIFF: 
https://github.com/llvm/llvm-project/commit/ac892c70a456a443ef5d52ec886b4b6b0fae9244.diff

LOG: [OMPIRBuilder] Add support for simdlen clause

This patch adds OMPIRBuilder support for the simdlen clause for the
simd directive. It uses the simdlen support in OpenMPIRBuilder when
it is enabled in Clang. Simdlen is lowered by OpenMPIRBuilder by
generating the loop.vectorize.width metadata.

Reviewed By: jdoerfert, Meinersbur

Differential Revision: https://reviews.llvm.org/D129149

Added: 
clang/test/OpenMP/irbuilder_simdlen.cpp

Modified: 
clang/lib/CodeGen/CGStmtOpenMP.cpp
clang/test/OpenMP/irbuilder_simd.cpp
llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
llvm/utils/UpdateTestChecks/common.py
mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp

Removed: 




diff  --git a/clang/lib/CodeGen/CGStmtOpenMP.cpp 
b/clang/lib/CodeGen/CGStmtOpenMP.cpp
index 63c446335c41a..db0b2ffd3a4f6 100644
--- a/clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -2591,11 +2591,12 @@ static void emitOMPSimdRegion(CodeGenFunction &CGF, 
const OMPLoopDirective &S,
   }
 }
 
-static bool isSupportedByOpenMPIRBuilder(const OMPExecutableDirective &S) {
+static bool isSupportedByOpenMPIRBuilder(const OMPSimdDirective &S) {
   // Check for unsupported clauses
-  if (!S.clauses().empty()) {
-// Currently no clause is supported
-return false;
+  for (OMPClause *C : S.clauses()) {
+// Currently only simdlen clause is supported
+if (!isa(C))
+  return false;
   }
 
   // Check if we have a statement with the ordered directive.
@@ -2630,7 +2631,6 @@ void CodeGenFunction::EmitOMPSimdDirective(const 
OMPSimdDirective &S) {
   // Use the OpenMPIRBuilder if enabled.
   if (UseOMPIRBuilder) {
 // Emit the associated statement and get its loop representation.
-llvm::DebugLoc DL = SourceLocToDebugLoc(S.getBeginLoc());
 const Stmt *Inner = S.getRawStmt();
 llvm::CanonicalLoopInfo *CLI =
 EmitOMPCollapsedCanonicalLoopNest(Inner, 1);
@@ -2638,7 +2638,15 @@ void CodeGenFunction::EmitOMPSimdDirective(const 
OMPSimdDirective &S) {
 llvm::OpenMPIRBuilder &OMPBuilder =
 CGM.getOpenMPRuntime().getOMPBuilder();
 // Add SIMD specific metadata
-OMPBuilder.applySimd(DL, CLI);
+llvm::ConstantInt *Simdlen = nullptr;
+if (const auto *C = S.getSingleClause()) {
+  RValue Len =
+  this->EmitAnyExpr(C->getSimdlen(), AggValueSlot::ignored(),
+/*ignoreResult=*/true);
+  auto *Val = cast(Len.getScalarVal());
+  Simdlen = Val;
+}
+OMPBuilder.applySimd(CLI, Simdlen);
 return;
   }
 };

diff  --git a/clang/test/OpenMP/irbuilder_simd.cpp 
b/clang/test/OpenMP/irbuilder_simd.cpp
index 2112c53049016..5fd5117f07a81 100644
--- a/clang/test/OpenMP/irbuilder_simd.cpp
+++ b/clang/test/OpenMP/irbuilder_simd.cpp
@@ -68,4 +68,4 @@ void simple(float *a, float *b, int *c) {
 // CHECK-NEXT: ![[META6]]  = !{!"llvm.loop.vectorize.enable", i1 true}
 // CHECK-NEXT: ![[META7:[0-9]+]] = distinct !{}
 // CHECK-NEXT: ![[META8]]  = distinct !{![[META8]], ![[META9:[0-9]+]], 
![[META6]]}
-// CHECK-NEXT: ![[META9]]  = !{!"llvm.loop.parallel_accesses", ![[META7]]}
\ No newline at end of file
+// CHECK-NEXT: ![[META9]]  = !{!"llvm.loop.parallel_accesses", ![[META7]]}

diff  --git a/clang/test/OpenMP/irbuilder_simdlen.cpp 
b/clang/test/OpenMP/irbuilder_simdlen.cpp
new file mode 100644
index 0..11714fac616d1
--- /dev/null
+++ b/clang/test/OpenMP/irbuilder_simdlen.cpp
@@ -0,0 +1,139 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py 
UTC_ARGS: --check-globals
+// RUN: %clang_cc1 -no-opaque-pointers -fopenmp-enable-irbuilder -verify 
-fopenmp -fopenmp-version=45 -x c++ -triple x86_64-unknown-unknown -emit-llvm 
%s -o - | FileCheck %s
+// expected-no-diagnostics
+
+struct S {
+  int a, b;
+};
+
+struct P {
+  int a, b;
+};
+
+// CHECK-LABEL: @_Z6simplePfS_Pi(
+// CHECK-NEXT:  entry:
+// CHECK-NEXT:[[A_ADDR:%.*]] = alloca float*, align 8
+// CHECK-NEXT:[[B_ADDR:%.*]] = alloca float*, align 8
+// CHECK-NEXT:[[C_ADDR:%.*]] = alloca i32*, align 8
+// CHECK-NEXT:[[S:%.*]] = alloca [[STRUCT_S:%.*]], align 4
+// CHECK-NEXT:[[P:%.*]] = alloca %struct.S*, align 8
+// CHECK-NEXT:[[PP:%.*]] = alloca [[STRUCT_P:%.*]], align 4
+// CHECK-NEXT:[[I:%.*]] = alloca i32, align 4
+// CHECK-NEXT:[[AGG_CAPTURED:%.*]] = alloca [[STRUCT_ANON:%.*]], align 8
+// CHECK-NEXT:[[

[clang] 1fd4bee - [flang][driver] Add -fdebug-module-writer option

2021-03-11 Thread Arnamoy Bhattacharyya via cfe-commits

Author: Arnamoy Bhattacharyya
Date: 2021-03-11T08:04:37-05:00
New Revision: 1fd4beecc8bb1148123265a63e0bff92b626c4a3

URL: 
https://github.com/llvm/llvm-project/commit/1fd4beecc8bb1148123265a63e0bff92b626c4a3
DIFF: 
https://github.com/llvm/llvm-project/commit/1fd4beecc8bb1148123265a63e0bff92b626c4a3.diff

LOG: [flang][driver] Add -fdebug-module-writer option

Added: 


Modified: 
clang/include/clang/Driver/Options.td
clang/lib/Driver/ToolChains/Flang.cpp
flang/include/flang/Frontend/CompilerInvocation.h
flang/lib/Frontend/CompilerInvocation.cpp
flang/lib/Frontend/FrontendActions.cpp
flang/test/Driver/driver-help.f90
flang/test/Semantics/mod-file-rewriter.f90

Removed: 




diff  --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 376a3baf0a4b..f4d4ece9baeb 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -4367,6 +4367,8 @@ def fdebug_measure_parse_tree : Flag<["-"], 
"fdebug-measure-parse-tree">, Group<
   HelpText<"Measure the parse tree">;
 def fdebug_pre_fir_tree : Flag<["-"], "fdebug-pre-fir-tree">, 
Group,
   HelpText<"Dump the pre-FIR tree">;
+def fdebug_module_writer : Flag<["-"],"fdebug-module-writer">, 
+  HelpText<"Enable debug messages while writing module files">;
 
 }
 

diff  --git a/clang/lib/Driver/ToolChains/Flang.cpp 
b/clang/lib/Driver/ToolChains/Flang.cpp
index 1a812589d473..153a1dfc8592 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -41,7 +41,8 @@ void Flang::AddPreprocessingOptions(const ArgList &Args,
 }
 
 void Flang::AddOtherOptions(const ArgList &Args, ArgStringList &CmdArgs) const 
{
-  Args.AddAllArgs(CmdArgs, options::OPT_module_dir);
+  Args.AddAllArgs(CmdArgs,
+  {options::OPT_module_dir, 
options::OPT_fdebug_module_writer});
 }
 
 void Flang::ConstructJob(Compilation &C, const JobAction &JA,

diff  --git a/flang/include/flang/Frontend/CompilerInvocation.h 
b/flang/include/flang/Frontend/CompilerInvocation.h
index 9946f3e4a6e6..3be6f3fc4d40 100644
--- a/flang/include/flang/Frontend/CompilerInvocation.h
+++ b/flang/include/flang/Frontend/CompilerInvocation.h
@@ -69,6 +69,8 @@ class CompilerInvocation : public CompilerInvocationBase {
   // of options.
   std::string moduleDir_ = ".";
 
+  bool debugModuleDir_ = false;
+
   // Fortran Dialect options
   Fortran::common::IntrinsicTypeDefaultKinds defaultKinds_;
 
@@ -91,6 +93,9 @@ class CompilerInvocation : public CompilerInvocationBase {
   std::string &moduleDir() { return moduleDir_; }
   const std::string &moduleDir() const { return moduleDir_; }
 
+  bool &debugModuleDir() { return debugModuleDir_; }
+  const bool &debugModuleDir() const { return debugModuleDir_; }
+
   Fortran::common::IntrinsicTypeDefaultKinds &defaultKinds() {
 return defaultKinds_;
   }
@@ -106,6 +111,11 @@ class CompilerInvocation : public CompilerInvocationBase {
   llvm::ArrayRef commandLineArgs,
   clang::DiagnosticsEngine &diags);
 
+  /// Useful setters
+  void SetModuleDir(std::string &moduleDir) { moduleDir_ = moduleDir; }
+
+  void SetDebugModuleDir(bool flag) { debugModuleDir_ = flag; }
+
   /// Set the Fortran options to predifined defaults. These defaults are
   /// consistend with f18/f18.cpp.
   // TODO: We should map frontendOpts_ to parserOpts_ instead. For that, we

diff  --git a/flang/lib/Frontend/CompilerInvocation.cpp 
b/flang/lib/Frontend/CompilerInvocation.cpp
index 17649700e0d2..1271cd314831 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -306,9 +306,10 @@ static void parsePreprocessorArgs(
 
 /// Parses all semantic related arguments and populates the variables
 /// options accordingly.
-static void parseSemaArgs(std::string &moduleDir, llvm::opt::ArgList &args,
+static void parseSemaArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
 clang::DiagnosticsEngine &diags) {
 
+  // -J/module-dir option
   auto moduleDirList =
   args.getAllArgValues(clang::driver::options::OPT_module_dir);
   // User can only specify -J/-module-dir once
@@ -320,7 +321,12 @@ static void parseSemaArgs(std::string &moduleDir, 
llvm::opt::ArgList &args,
 diags.Report(diagID);
   }
   if (moduleDirList.size() == 1)
-moduleDir = moduleDirList[0];
+res.SetModuleDir(moduleDirList[0]);
+
+  // -fdebug-module-writer option
+  if (args.hasArg(clang::driver::options::OPT_fdebug_module_writer)) {
+res.SetDebugModuleDir(true);
+  }
 }
 
 /// Parses all Dialect related arguments and populates the variables
@@ -395,7 +401,7 @@ bool CompilerInvocation::CreateFromArgs(CompilerInvocation 
&res,
   // Parse the preprocessor args
   parsePreprocessorArgs(res.preprocessorOpts(), args);
   // Parse semantic args
-  parseSemaArgs(res.moduleDir(), args, diags);
+  parseSemaArgs(res, args, diags);
   // Parse d

[clang] 9fbd33a - [OMPIRBuilder] Add support for simd (loop) directive.

2022-01-19 Thread Arnamoy Bhattacharyya via cfe-commits

Author: Arnamoy Bhattacharyya
Date: 2022-01-19T11:32:17-05:00
New Revision: 9fbd33ad623d2b576fc563545bbdf2c257cdf709

URL: 
https://github.com/llvm/llvm-project/commit/9fbd33ad623d2b576fc563545bbdf2c257cdf709
DIFF: 
https://github.com/llvm/llvm-project/commit/9fbd33ad623d2b576fc563545bbdf2c257cdf709.diff

LOG: [OMPIRBuilder] Add support for simd (loop) directive.

This patch adds OMPIRBuilder support for the simd directive (without any 
clause).  This will be a first step towards lowering simd directive in 
LLVM_Flang.  The patch uses existing CanonicalLoop infrastructure of IRBuilder 
to add the support.  Also adds necessary code to add llvm.access.group and 
llvm.loop metadata wherever needed.

Reviewed By: Meinersbur

Differential Revision: https://reviews.llvm.org/D114379

Added: 
clang/test/OpenMP/irbuilder_simd.cpp

Modified: 
clang/lib/CodeGen/CGStmtOpenMP.cpp
llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp

Removed: 




diff  --git a/clang/lib/CodeGen/CGStmtOpenMP.cpp 
b/clang/lib/CodeGen/CGStmtOpenMP.cpp
index 11af2812a4118..0db59dd2624c5 100644
--- a/clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -2584,7 +2584,67 @@ static void emitOMPSimdRegion(CodeGenFunction &CGF, 
const OMPLoopDirective &S,
   }
 }
 
+static bool isSupportedByOpenMPIRBuilder(const OMPExecutableDirective &S) {
+  // Check for unsupported clauses
+  if (!S.clauses().empty()) {
+// Currently no clause is supported
+return false;
+  }
+
+  // Check if we have a statement with the ordered directive.
+  // Visit the statement hierarchy to find a compound statement
+  // with a ordered directive in it.
+  if (const auto *CanonLoop = dyn_cast(S.getRawStmt())) {
+if (const Stmt *SyntacticalLoop = CanonLoop->getLoopStmt()) {
+  for (const Stmt *SubStmt : SyntacticalLoop->children()) {
+if (!SubStmt)
+  continue;
+if (const CompoundStmt *CS = dyn_cast(SubStmt)) {
+  for (const Stmt *CSSubStmt : CS->children()) {
+if (!CSSubStmt)
+  continue;
+if (isa(CSSubStmt)) {
+  return false;
+}
+  }
+}
+  }
+}
+  }
+  return true;
+}
+
 void CodeGenFunction::EmitOMPSimdDirective(const OMPSimdDirective &S) {
+  bool UseOMPIRBuilder =
+  CGM.getLangOpts().OpenMPIRBuilder && isSupportedByOpenMPIRBuilder(S);
+  if (UseOMPIRBuilder) {
+auto &&CodeGenIRBuilder = [this, &S, UseOMPIRBuilder](CodeGenFunction &CGF,
+  PrePostActionTy &) {
+  // Use the OpenMPIRBuilder if enabled.
+  if (UseOMPIRBuilder) {
+// Emit the associated statement and get its loop representation.
+llvm::DebugLoc DL = SourceLocToDebugLoc(S.getBeginLoc());
+const Stmt *Inner = S.getRawStmt();
+llvm::CanonicalLoopInfo *CLI =
+EmitOMPCollapsedCanonicalLoopNest(Inner, 1);
+
+llvm::OpenMPIRBuilder &OMPBuilder =
+CGM.getOpenMPRuntime().getOMPBuilder();
+// Add SIMD specific metadata
+OMPBuilder.applySimd(DL, CLI);
+return;
+  }
+};
+{
+  auto LPCRegion =
+  CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S);
+  OMPLexicalScope Scope(*this, S, OMPD_unknown);
+  CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd,
+  CodeGenIRBuilder);
+}
+return;
+  }
+
   ParentLoopDirectiveForScanRegion ScanRegion(*this, S);
   OMPFirstScanLoop = true;
   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {

diff  --git a/clang/test/OpenMP/irbuilder_simd.cpp 
b/clang/test/OpenMP/irbuilder_simd.cpp
new file mode 100644
index 0..9f369290f2db5
--- /dev/null
+++ b/clang/test/OpenMP/irbuilder_simd.cpp
@@ -0,0 +1,71 @@
+// RUN: %clang_cc1 -fopenmp-enable-irbuilder -verify -fopenmp 
-fopenmp-version=45 -x c++ -triple x86_64-unknown-unknown -emit-llvm %s -o - | 
FileCheck %s 
+// expected-no-diagnostics
+
+struct S {
+  int a, b;
+};
+
+struct P {
+  int a, b;
+};
+
+void simple(float *a, float *b, int *c) {
+  S s, *p;
+  P pp;
+#pragma omp simd
+  for (int i = 3; i < 32; i += 5) {
+// llvm.access.group test
+// CHECK: %[[A_ADDR:.+]] = alloca float*, align 8
+// CHECK: %[[B_ADDR:.+]] = alloca float*, align 8
+// CHECK: %[[S:.+]] = alloca %struct.S, align 4
+// CHECK: %[[P:.+]] = alloca %struct.S*, align 8
+// CHECK: %[[I:.+]] = alloca i32, align 4
+// CHECK: %[[TMP3:.+]] = load float*, float** %[[B_ADDR:.+]], align 8, 
!llvm.access.group ![[META3:[0-9]+]]
+// CHECK-NEXT: %[[TMP4:.+]] = load i32, i32* %[[I:.+]], align 4, 
!llvm.access.group ![[META3:[0-9]+]]
+// CHECK-NEXT: %[[IDXPROM:.+]] = sext i32 %[[TMP4:.+]] to i64
+// CH

[clang] cd4abc5 - [flang][driver] Add -fintrinsic-modules-path option

2021-03-23 Thread Arnamoy Bhattacharyya via cfe-commits

Author: Arnamoy Bhattacharyya
Date: 2021-03-23T12:28:19-04:00
New Revision: cd4abc5242c03804b3d88277b03b52215a899f75

URL: 
https://github.com/llvm/llvm-project/commit/cd4abc5242c03804b3d88277b03b52215a899f75
DIFF: 
https://github.com/llvm/llvm-project/commit/cd4abc5242c03804b3d88277b03b52215a899f75.diff

LOG: [flang][driver] Add -fintrinsic-modules-path option

Reviewed By: awarzynski

Differential Revision: https://reviews.llvm.org/D97080

Added: 
flang/test/Driver/Inputs/ieee_arithmetic.mod
flang/test/Driver/Inputs/iso_fortran_env.mod
flang/test/Driver/intrinsic_module_path.f90

Modified: 
clang/include/clang/Driver/Options.td
clang/lib/Driver/ToolChains/Flang.cpp
flang/include/flang/Frontend/PreprocessorOptions.h
flang/lib/Frontend/CompilerInvocation.cpp
flang/test/Driver/driver-help-hidden.f90
flang/test/Driver/driver-help.f90

Removed: 




diff  --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 2e9d0f53f9a31..25de15f484952 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -4256,7 +4256,6 @@ defm f2c : BooleanFFlag<"f2c">, Group;
 defm frontend_optimize : BooleanFFlag<"frontend-optimize">, 
Group;
 defm init_local_zero : BooleanFFlag<"init-local-zero">, Group;
 defm integer_4_integer_8 : BooleanFFlag<"integer-4-integer-8">, 
Group;
-defm intrinsic_modules_path : BooleanFFlag<"intrinsic-modules-path">, 
Group;
 defm max_identifier_length : BooleanFFlag<"max-identifier-length">, 
Group;
 defm module_private : BooleanFFlag<"module-private">, Group;
 defm pack_derived : BooleanFFlag<"pack-derived">, Group;
@@ -4338,6 +4337,10 @@ def fimplicit_none : Flag<["-"], "fimplicit-none">, 
Group,
 def fno_implicit_none : Flag<["-"], "fno-implicit-none">, Group;
 def falternative_parameter_statement : Flag<["-"], 
"falternative-parameter-statement">, Group,
   HelpText<"Enable the old style PARAMETER statement">;
+def fintrinsic_modules_path : Separate<["-"], "fintrinsic-modules-path">,  
Group, MetaVarName<"">,
+  HelpText<"Specify where to find the compiled intrinsic modules">,
+  DocBrief<[{This option specifies the location of pre-compiled intrinsic 
modules, 
+  if they are not in the default location expected by the compiler.}]>;
 }
 
 def J : JoinedOrSeparate<["-"], "J">,

diff  --git a/clang/lib/Driver/ToolChains/Flang.cpp 
b/clang/lib/Driver/ToolChains/Flang.cpp
index 153a1dfc85927..1fa62030b1134 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -42,7 +42,8 @@ void Flang::AddPreprocessingOptions(const ArgList &Args,
 
 void Flang::AddOtherOptions(const ArgList &Args, ArgStringList &CmdArgs) const 
{
   Args.AddAllArgs(CmdArgs,
-  {options::OPT_module_dir, 
options::OPT_fdebug_module_writer});
+  {options::OPT_module_dir, options::OPT_fdebug_module_writer,
+   options::OPT_fintrinsic_modules_path});
 }
 
 void Flang::ConstructJob(Compilation &C, const JobAction &JA,

diff  --git a/flang/include/flang/Frontend/PreprocessorOptions.h 
b/flang/include/flang/Frontend/PreprocessorOptions.h
index 39ea4d3d3c6cf..3a3877bf0b286 100644
--- a/flang/include/flang/Frontend/PreprocessorOptions.h
+++ b/flang/include/flang/Frontend/PreprocessorOptions.h
@@ -29,6 +29,8 @@ class PreprocessorOptions {
   // consider collecting them in a separate aggregate. For now we keep it here
   // as there is no point creating a class for just one field.
   std::vector searchDirectoriesFromDashI;
+  // Search directories specified by the user with -fintrinsic-modules-path
+  std::vector searchDirectoriesFromIntrModPath;
 
 public:
   PreprocessorOptions() {}
@@ -44,4 +46,4 @@ class PreprocessorOptions {
 
 } // namespace Fortran::frontend
 
-#endif // LLVM_FLANG_PREPROCESSOROPTIONS_H
\ No newline at end of file
+#endif // LLVM_FLANG_PREPROCESSOROPTIONS_H

diff  --git a/flang/lib/Frontend/CompilerInvocation.cpp 
b/flang/lib/Frontend/CompilerInvocation.cpp
index d2318d3d683d2..69c78bde7ff11 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -21,6 +21,8 @@
 #include "llvm/Option/Arg.h"
 #include "llvm/Option/ArgList.h"
 #include "llvm/Option/OptTable.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/FileUtilities.h"
 #include "llvm/Support/Process.h"
 #include "llvm/Support/raw_ostream.h"
 #include 
@@ -285,6 +287,16 @@ static InputKind ParseFrontendArgs(FrontendOptions &opts,
   return dashX;
 }
 
+// Generate the path to look for intrinsic modules
+static std::string getIntrinsicDir() {
+  // TODO: Find a system independent API
+  llvm::SmallString<128> driverPath;
+  driverPath.assign(llvm::sys::fs::getMainExecutable(nullptr, nullptr));
+  llvm::sys::path::remove_filename(driverPath);
+  driverPath.append("/../include/flang/");
+  return std::string(driverPath);
+}
+

[clang] 4c7ebf7 - [flang][driver] Add options for -std=f2018

2021-03-25 Thread Arnamoy Bhattacharyya via cfe-commits

Author: Arnamoy Bhattacharyya
Date: 2021-03-25T13:03:16-04:00
New Revision: 4c7ebf79e923072e8d298134e6ca04618fe4eba9

URL: 
https://github.com/llvm/llvm-project/commit/4c7ebf79e923072e8d298134e6ca04618fe4eba9
DIFF: 
https://github.com/llvm/llvm-project/commit/4c7ebf79e923072e8d298134e6ca04618fe4eba9.diff

LOG: [flang][driver] Add options for -std=f2018

Reviewed By: awarzynski

Differential Revision: https://reviews.llvm.org/D97119

Added: 
flang/test/Driver/std2018.f90
flang/test/Driver/std2018_wrong.f90

Modified: 
clang/include/clang/Driver/Options.td
clang/lib/Driver/ToolChains/Flang.cpp
flang/include/flang/Frontend/CompilerInvocation.h
flang/lib/Frontend/CompilerInvocation.cpp
flang/test/Driver/driver-help-hidden.f90
flang/test/Driver/driver-help.f90
flang/tools/f18/f18.cpp

Removed: 




diff  --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index f4af1a4b10f1..8ee3ebf7f2af 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -3535,8 +3535,8 @@ def pagezero__size : JoinedOrSeparate<["-"], 
"pagezero_size">;
 def pass_exit_codes : Flag<["-", "--"], "pass-exit-codes">, 
Flags<[Unsupported]>;
 def pedantic_errors : Flag<["-", "--"], "pedantic-errors">, 
Group, Flags<[CC1Option]>,
   MarshallingInfoFlag>;
-def pedantic : Flag<["-", "--"], "pedantic">, Group, 
Flags<[CC1Option]>,
-  MarshallingInfoFlag>;
+def pedantic : Flag<["-", "--"], "pedantic">, Group, 
Flags<[CC1Option,FlangOption,FC1Option]>,
+  HelpText<"Warn on language extensions">, 
MarshallingInfoFlag>;
 def pg : Flag<["-"], "pg">, HelpText<"Enable mcount instrumentation">, 
Flags<[CC1Option]>,
   MarshallingInfoFlag>;
 def pipe : Flag<["-", "--"], "pipe">,
@@ -3638,7 +3638,7 @@ def static_libgcc : Flag<["-"], "static-libgcc">;
 def static_libstdcxx : Flag<["-"], "static-libstdc++">;
 def static : Flag<["-", "--"], "static">, Group, 
Flags<[NoArgumentUnused]>;
 def std_default_EQ : Joined<["-"], "std-default=">;
-def std_EQ : Joined<["-", "--"], "std=">, Flags<[CC1Option]>,
+def std_EQ : Joined<["-", "--"], "std=">, 
Flags<[CC1Option,FlangOption,FC1Option]>,
   Group, HelpText<"Language standard to compile for">,
   ValuesCode<[{
 const char *Values =

diff  --git a/clang/lib/Driver/ToolChains/Flang.cpp 
b/clang/lib/Driver/ToolChains/Flang.cpp
index 1fa62030b113..bf2a19e7c54a 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -43,7 +43,8 @@ void Flang::AddPreprocessingOptions(const ArgList &Args,
 void Flang::AddOtherOptions(const ArgList &Args, ArgStringList &CmdArgs) const 
{
   Args.AddAllArgs(CmdArgs,
   {options::OPT_module_dir, options::OPT_fdebug_module_writer,
-   options::OPT_fintrinsic_modules_path});
+   options::OPT_fintrinsic_modules_path, options::OPT_pedantic,
+   options::OPT_std_EQ});
 }
 
 void Flang::ConstructJob(Compilation &C, const JobAction &JA,

diff  --git a/flang/include/flang/Frontend/CompilerInvocation.h 
b/flang/include/flang/Frontend/CompilerInvocation.h
index 3be6f3fc4d40..99050dcdbd7b 100644
--- a/flang/include/flang/Frontend/CompilerInvocation.h
+++ b/flang/include/flang/Frontend/CompilerInvocation.h
@@ -74,6 +74,8 @@ class CompilerInvocation : public CompilerInvocationBase {
   // Fortran Dialect options
   Fortran::common::IntrinsicTypeDefaultKinds defaultKinds_;
 
+  bool EnableConformanceChecks_ = false;
+
 public:
   CompilerInvocation() = default;
 
@@ -96,6 +98,11 @@ class CompilerInvocation : public CompilerInvocationBase {
   bool &debugModuleDir() { return debugModuleDir_; }
   const bool &debugModuleDir() const { return debugModuleDir_; }
 
+  bool &enableConformanceChecks() { return EnableConformanceChecks_; }
+  const bool &enableConformanceChecks() const {
+return EnableConformanceChecks_;
+  }
+
   Fortran::common::IntrinsicTypeDefaultKinds &defaultKinds() {
 return defaultKinds_;
   }
@@ -111,6 +118,9 @@ class CompilerInvocation : public CompilerInvocationBase {
   llvm::ArrayRef commandLineArgs,
   clang::DiagnosticsEngine &diags);
 
+  // Enables the std=f2018 conformance check
+  void set_EnableConformanceChecks() { EnableConformanceChecks_ = true; }
+
   /// Useful setters
   void SetModuleDir(std::string &moduleDir) { moduleDir_ = moduleDir; }
 

diff  --git a/flang/lib/Frontend/CompilerInvocation.cpp 
b/flang/lib/Frontend/CompilerInvocation.cpp
index 69c78bde7ff1..6d0003e79571 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -389,6 +389,26 @@ static void parseDialectArgs(CompilerInvocation &res, 
llvm::opt::ArgList &args,
 res.frontendOpts().features_.Enable(
 Fortran::common::LanguageFeature::OpenMP);
   }
+
+  //-fpedantic
+  if (args.hasArg(clang::driver::options::OPT_pedantic)) {
+res.

[clang] 7416e8a - [flang][driver] Add options for -Werror

2021-04-05 Thread Arnamoy Bhattacharyya via cfe-commits

Author: Arnamoy Bhattacharyya
Date: 2021-04-05T12:47:52-04:00
New Revision: 7416e8a8431a0f2711be9d16e111d1781b74df96

URL: 
https://github.com/llvm/llvm-project/commit/7416e8a8431a0f2711be9d16e111d1781b74df96
DIFF: 
https://github.com/llvm/llvm-project/commit/7416e8a8431a0f2711be9d16e111d1781b74df96.diff

LOG: [flang][driver] Add options for -Werror

With the option given, warnings are treated as error.

Reviewed By: awarzynski

Differential Revision: https://reviews.llvm.org/D98657

Added: 
flang/test/Driver/werror_parse.f
flang/test/Driver/werror_scan.f
flang/test/Driver/werror_sema.f90
flang/test/Driver/werror_wrong.f90

Modified: 
clang/include/clang/Driver/Options.td
clang/lib/Driver/ToolChains/Flang.cpp
flang/include/flang/Frontend/CompilerInvocation.h
flang/lib/Frontend/CompilerInvocation.cpp
flang/lib/Frontend/FrontendActions.cpp
flang/test/Driver/driver-help-hidden.f90
flang/test/Driver/driver-help.f90
flang/tools/f18/f18.cpp

Removed: 




diff  --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 64d658b2bfd9..38977cc0b874 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -746,7 +746,7 @@ def Wundef_prefix_EQ : CommaJoined<["-"], 
"Wundef-prefix=">, Group>;
 def Wwrite_strings : Flag<["-"], "Wwrite-strings">, Group, 
Flags<[CC1Option, HelpHidden]>;
 def Wno_write_strings : Flag<["-"], "Wno-write-strings">, Group, 
Flags<[CC1Option, HelpHidden]>;
-def W_Joined : Joined<["-"], "W">, Group, Flags<[CC1Option, 
CoreOption]>,
+def W_Joined : Joined<["-"], "W">, Group, Flags<[CC1Option, 
CoreOption, FC1Option, FlangOption]>,
   MetaVarName<"">, HelpText<"Enable the specified warning">;
 def Xanalyzer : Separate<["-"], "Xanalyzer">,
   HelpText<"Pass  to the static analyzer">, MetaVarName<"">,

diff  --git a/clang/lib/Driver/ToolChains/Flang.cpp 
b/clang/lib/Driver/ToolChains/Flang.cpp
index bf2a19e7c54a..73dbeacf2563 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -44,7 +44,7 @@ void Flang::AddOtherOptions(const ArgList &Args, 
ArgStringList &CmdArgs) const {
   Args.AddAllArgs(CmdArgs,
   {options::OPT_module_dir, options::OPT_fdebug_module_writer,
options::OPT_fintrinsic_modules_path, options::OPT_pedantic,
-   options::OPT_std_EQ});
+   options::OPT_std_EQ, options::OPT_W_Joined});
 }
 
 void Flang::ConstructJob(Compilation &C, const JobAction &JA,

diff  --git a/flang/include/flang/Frontend/CompilerInvocation.h 
b/flang/include/flang/Frontend/CompilerInvocation.h
index 99050dcdbd7b..529f15e3c886 100644
--- a/flang/include/flang/Frontend/CompilerInvocation.h
+++ b/flang/include/flang/Frontend/CompilerInvocation.h
@@ -71,6 +71,8 @@ class CompilerInvocation : public CompilerInvocationBase {
 
   bool debugModuleDir_ = false;
 
+  bool warnAsErr_ = false;
+
   // Fortran Dialect options
   Fortran::common::IntrinsicTypeDefaultKinds defaultKinds_;
 
@@ -98,6 +100,9 @@ class CompilerInvocation : public CompilerInvocationBase {
   bool &debugModuleDir() { return debugModuleDir_; }
   const bool &debugModuleDir() const { return debugModuleDir_; }
 
+  bool &warnAsErr() { return warnAsErr_; }
+  const bool &warnAsErr() const { return warnAsErr_; }
+
   bool &enableConformanceChecks() { return EnableConformanceChecks_; }
   const bool &enableConformanceChecks() const {
 return EnableConformanceChecks_;
@@ -126,6 +131,8 @@ class CompilerInvocation : public CompilerInvocationBase {
 
   void SetDebugModuleDir(bool flag) { debugModuleDir_ = flag; }
 
+  void SetWarnAsErr(bool flag) { warnAsErr_ = flag; }
+
   /// Set the Fortran options to predifined defaults. These defaults are
   /// consistend with f18/f18.cpp.
   // TODO: We should map frontendOpts_ to parserOpts_ instead. For that, we

diff  --git a/flang/lib/Frontend/CompilerInvocation.cpp 
b/flang/lib/Frontend/CompilerInvocation.cpp
index 3bd541e40c0f..ce7392cf3d76 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -350,6 +350,26 @@ static void parseSemaArgs(CompilerInvocation &res, 
llvm::opt::ArgList &args,
   }
 }
 
+/// Parses all diagnostics related arguments and populates the variables
+/// options accordingly.
+static void parseDiagArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
+clang::DiagnosticsEngine &diags) {
+  // -Werror option
+  // TODO: Currently throws a Diagnostic for anything other than -W,
+  // this has to change when other -W's are supported.
+  if (args.hasArg(clang::driver::options::OPT_W_Joined)) {
+if (args.getLastArgValue(clang::driver::options::OPT_W_Joined)
+.equals("error")) {
+  res.SetWarnAsErr(true);
+} else {
+  const unsigned diagID =
+  diags.getCustomDiagID(clang::DiagnosticsEngi