Skip to content

Conversation

@eugeneepshteyn
Copy link
Contributor

@eugeneepshteyn eugeneepshteyn commented Dec 15, 2025

If -funsafe-cray-pointers is passed to flang driver, pass -mmlir -funsafe-cray-pointers to the compiler. This should make it easier to work with the code that relies on behavior before #170908

If -funsafe-cray-pointers is passed to flang driver, pass
'-mmlir -funsafe-cray-pointers' to the compiler.
@github-actions
Copy link

github-actions bot commented Dec 15, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@eugeneepshteyn eugeneepshteyn marked this pull request as ready for review December 16, 2025 02:34
@llvmbot llvmbot added clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' flang:driver flang Flang issues not falling into any other category labels Dec 16, 2025
@llvmbot
Copy link
Member

llvmbot commented Dec 16, 2025

@llvm/pr-subscribers-flang-driver

@llvm/pr-subscribers-clang-driver

Author: Eugene Epshteyn (eugeneepshteyn)

Changes

If -funsafe-cray-pointers is passed to flang driver, pass '-mmlir -funsafe-cray-pointers' to the compiler.


Full diff: https://github.com/llvm/llvm-project/pull/172252.diff

3 Files Affected:

  • (modified) clang/include/clang/Options/Options.td (+4)
  • (modified) clang/lib/Driver/ToolChains/Flang.cpp (+7)
  • (added) flang/test/Driver/unsafe-cray-pointers.f90 (+5)
diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td
index c38bdd06057a2..9c3b284c4967c 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -7457,6 +7457,10 @@ By default, the heap is used. Allocations of polymorphic types
 are always done on the heap, though this may change in future releases.
   }]>;
 
+defm unsafe_cray_pointers : BoolOptionWithoutMarshalling<"f", "unsafe-cray-pointers",
+  PosFlag<SetTrue, [], [FlangOption, FC1Option], "Optimizations allow for unsafe Cray pointer usages">,
+  NegFlag<SetFalse, [], [FlangOption, FC1Option], "Optimizations don't allow for unsafe Cray pointer usages (default)">>;
+
 def fhermetic_module_files : Flag<["-"], "fhermetic-module-files">, Group<f_Group>,
   HelpText<"Emit hermetic module files (no nested USE association)">;
 
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index 2f5e93d139858..4e7ab2cf6edc3 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -201,6 +201,13 @@ void Flang::addCodegenOptions(const ArgList &Args,
       !stackArrays->getOption().matches(options::OPT_fno_stack_arrays))
     CmdArgs.push_back("-fstack-arrays");
 
+  if (Args.hasFlag(options::OPT_funsafe_cray_pointers,
+                   options::OPT_fno_unsafe_cray_pointers, false)) {
+    // TODO: currently passed as MLIR option
+    CmdArgs.push_back("-mmlir");
+    CmdArgs.push_back("-funsafe-cray-pointers");
+  }
+
   Args.addOptInFlag(CmdArgs, options::OPT_fexperimental_loop_fusion,
                     options::OPT_fno_experimental_loop_fusion);
 
diff --git a/flang/test/Driver/unsafe-cray-pointers.f90 b/flang/test/Driver/unsafe-cray-pointers.f90
new file mode 100644
index 0000000000000..bd8cafc1602f3
--- /dev/null
+++ b/flang/test/Driver/unsafe-cray-pointers.f90
@@ -0,0 +1,5 @@
+! RUN: %flang -funsafe-cray-pointers -### %s 2>&1 | FileCheck %s --check-prefix=CHECK-ON
+! RUN: %flang -fno-unsafe-cray-pointers -### %s 2>&1 | FileCheck %s --check-prefix=CHECK-OFF
+
+! CHECK-ON: "-mmlir" "-funsafe-cray-pointers"
+! CHECK-OFF-NOT: "-mmlir" "-funsafe-cray-pointers"

Copy link
Contributor

@vzakhari vzakhari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' flang:driver flang Flang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants