Skip to content

[flang] Ensure that the integer for Cray pointer is sized correctly #140822

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

eugeneepshteyn
Copy link
Contributor

The integer used for Cray pointers should have the size equivalent to platform's pointer size.

The integer used for Cray pointers should have the size equivalent to
platform's pointer size.

	modified:   flang/include/flang/Evaluate/target.h
	modified:   flang/include/flang/Tools/TargetSetup.h
	modified:   flang/lib/Semantics/resolve-names.cpp
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels May 20, 2025
@llvmbot
Copy link
Member

llvmbot commented May 20, 2025

@llvm/pr-subscribers-flang-semantics

Author: Eugene Epshteyn (eugeneepshteyn)

Changes

The integer used for Cray pointers should have the size equivalent to platform's pointer size.


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

3 Files Affected:

  • (modified) flang/include/flang/Evaluate/target.h (+6)
  • (modified) flang/include/flang/Tools/TargetSetup.h (+3)
  • (modified) flang/lib/Semantics/resolve-names.cpp (+1-1)
diff --git a/flang/include/flang/Evaluate/target.h b/flang/include/flang/Evaluate/target.h
index cc6172b492b3c..281e42d5285fb 100644
--- a/flang/include/flang/Evaluate/target.h
+++ b/flang/include/flang/Evaluate/target.h
@@ -131,6 +131,11 @@ class TargetCharacteristics {
   IeeeFeatures &ieeeFeatures() { return ieeeFeatures_; }
   const IeeeFeatures &ieeeFeatures() const { return ieeeFeatures_; }
 
+  std::size_t pointerSize() { return pointerSize_; }
+  void set_pointerSize(std::size_t pointerSize) {
+    pointerSize_ = pointerSize;
+  }
+
 private:
   static constexpr int maxKind{common::maxKind};
   std::uint8_t byteSize_[common::TypeCategory_enumSize][maxKind + 1]{};
@@ -156,6 +161,7 @@ class TargetCharacteristics {
       IeeeFeature::Io, IeeeFeature::NaN, IeeeFeature::Rounding,
       IeeeFeature::Sqrt, IeeeFeature::Standard, IeeeFeature::Subnormal,
       IeeeFeature::UnderflowControl};
+  std::size_t pointerSize_{8 /* bytes */};
 };
 
 } // namespace Fortran::evaluate
diff --git a/flang/include/flang/Tools/TargetSetup.h b/flang/include/flang/Tools/TargetSetup.h
index de3fb0519cf6b..a30c725e21fb4 100644
--- a/flang/include/flang/Tools/TargetSetup.h
+++ b/flang/include/flang/Tools/TargetSetup.h
@@ -94,6 +94,9 @@ namespace Fortran::tools {
   if (targetTriple.isOSWindows())
     targetCharacteristics.set_isOSWindows(true);
 
+  targetCharacteristics.set_pointerSize(
+    targetTriple.getArchPointerBitWidth() / 8);
+
   // TODO: use target machine data layout to set-up the target characteristics
   // type size and alignment info.
 }
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 92a3277191ae0..2f6fe4fea4da2 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -6690,7 +6690,7 @@ void DeclarationVisitor::Post(const parser::BasedPointer &bp) {
   }
   pointer->set(Symbol::Flag::CrayPointer);
   const DeclTypeSpec &pointerType{MakeNumericType(
-      TypeCategory::Integer, context().defaultKinds().subscriptIntegerKind())};
+      TypeCategory::Integer, context().targetCharacteristics().pointerSize())};
   const auto *type{pointer->GetType()};
   if (!type) {
     pointer->SetType(pointerType);

Copy link

github-actions bot commented May 20, 2025

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

Copy link
Contributor

@klausler klausler left a comment

Choose a reason for hiding this comment

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

The names in this code assumes that integer kinds equal their byte sizes. Steve has been wanting me to make the kind/size relationship configurable, or to at least not always have that assumption by having mapping functions.

integerKindForPointer or something along those lines would avoid that assumption. We don't really need a byte size here, since it's just a selection of an integer kind.

@kkwli
Copy link
Collaborator

kkwli commented May 21, 2025

Looks good. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants