Skip to content

Conversation

@valerydmit
Copy link
Contributor

@valerydmit valerydmit commented Nov 22, 2025

show_descriptor intrinsic prints details of a descriptor (extended Fortran pointer).
It accepts a descriptor for any type and rank, including scalars.
Requires use of flang_debug module.

Example:
program test
use flang_debug
implicit none
integer :: a(4) = (/ 1,3,5,7 /)
call show_descriptor(a(1:3))
end program test

and its output:
Descriptor @ 0x7ffe01ec6a98:
base_addr 0x563b7035103c
elem_len 4
version 20240719
rank 1
type 9 "INTEGER(kind=4)"
attribute 0
extra 0
addendum 0
alloc_idx 0
dim[0] lower_bound 1
extent 3
sm 4

@github-actions
Copy link

github-actions bot commented Nov 22, 2025

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

@valerydmit
Copy link
Contributor Author

valerydmit commented Nov 22, 2025

Output examples:

Descriptor @ 0x7ffcec05b7e8:
  base_addr 0x555f2bf9e070
  elem_len  4
  version   20240719
  scalar
  type      27 "REAL(kind=4)"
  attribute 0
  extra     0
    addendum  0
    alloc_idx 0
Descriptor @ 0x7ffcec05b7b8:
  base_addr 0x555f2bf9e074
  elem_len  4
  version   20240719
  rank      1
  type      27 "REAL(kind=4)"
  attribute 0
  extra     0
    addendum  0
    alloc_idx 0
  dim[0] lower_bound 1
         extent      3
         sm          4
Descriptor @ 0x7ffcec05b758:
  base_addr 0x555f2bf9e0a0
  elem_len  8
  version   20240719
  rank      1
  type      34 "COMPLEX(kind=4)"
  attribute 0
  extra     0
    addendum  0
    alloc_idx 0
  dim[0] lower_bound 1
         extent      2
         sm          8
Descriptor @ 0x7ffcec05b758:
  base_addr 0x555f2bf9e0b0
  elem_len  12
  version   20240719
  scalar
  type      42 "DERIVED"
  attribute 0
  extra     1
    addendum  1
    alloc_idx 0
  derivedType @ 0x555f2bf9d980

@github-actions
Copy link

github-actions bot commented Nov 22, 2025

🐧 Linux x64 Test Results

  • 4085 tests passed
  • 202 tests skipped

}

void Descriptor::Dump(FILE *f) const {
static const char *GetTypeStr(ISO::CFI_type_t type, bool dumpRawType) {
Copy link
Member

Choose a reason for hiding this comment

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

StringRef instead if const char*?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using llvm::StringRef would be a precedent for flang-rt.

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.

Thank you, Valery! It looks good to me.

I have one question inlined. Can you please also add unit tests into flang-rt/unittests/Runtime/?

Copy link
Contributor

@jeanPerier jeanPerier left a comment

Choose a reason for hiding this comment

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

Thanks for adding this, just some high level feedback:

I think I would slightly favor it being defined in an interface in some new intrinsic flang_debug module for better control/error messages when people are switching between compilers (@klausler for opinions).

I understand that defining it fully via Fortran without some interception in semantics is likely hard (probably require some combo of IGNORE_TKR).

However, maybe defining it via renaming to a builtin is an option (see how this is done for __builtin_c_loc for instance).

Second point is that POINTER/ALLOCATABLE descriptors will print the address of a copy (because of the fir.load) and I think it may be useful to get the real address for those. However, this can easily be improved later by updating lowering (adding an entry point if needed to satisfy FIR type between fir.box and fir.ref<fir.box>) and is not a blocking point.

@eugeneepshteyn
Copy link
Contributor

@valerydmit
Copy link
Contributor Author

Thank you for all your inputs. I tried to address all the raised concerns (except handling pointer/allocatable, which I'd like to do in a follow up patch).

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.

Thank you for the tests, Valery!

It LGTM, but please let Jean review the lowering part.

Copy link
Contributor

@jeanPerier jeanPerier left a comment

Choose a reason for hiding this comment

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

Nit on the test, LGTM otherwise, thanks for the update.

! CHECK: %[[LOAD_1:.*]] = fir.load %[[DECLARE_0]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
! CHECK: fir.call @_FortranAShowDescriptor(%[[LOAD_1]]) fastmath<contract> : (!fir.box<!fir.heap<!fir.array<?xi32>>>) -> ()

call __builtin_show_descriptor(a(1:3))
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you update the test to do use flang_debug and use show_descriptor?

User should not use the flang __builtin directly, and it would be good if the test also exercise the new module (I think bcc should pick the intrinsic modules, if it does not you can update the test to use flang_fc1).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Thanks

@valerydmit valerydmit marked this pull request as ready for review November 26, 2025 17:11
Copy link
Contributor

@jeanPerier jeanPerier left a comment

Choose a reason for hiding this comment

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

Thanks for the test update, LGTM

@valerydmit valerydmit merged commit e7748e9 into llvm:main Dec 1, 2025
13 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Dec 2, 2025

LLVM Buildbot has detected a new failure on builder flang-x86_64-windows running on minipc-ryzen-win while building flang-rt,flang at step 8 "test-build-unified-tree-check-flang-rt".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/166/builds/4535

Here is the relevant piece of the build log for the reference
Step 8 (test-build-unified-tree-check-flang-rt) failure: test (failure)
...
   79 |   if (const ::testing::AssertionResult gtest_ar = (expression)) \
      |                                                    ^
1 warning generated.
618.845 [32/6/295] Building CXX object flang-rt/unittests/Runtime/CMakeFiles/RuntimeTests.dir/Complex.cpp.obj
620.282 [31/6/296] Building CXX object flang-rt/unittests/Runtime/CMakeFiles/RuntimeTests.dir/CharacterTest.cpp.obj
620.510 [30/6/297] Building CXX object flang-rt/lib/runtime/CMakeFiles/flang_rt.runtime.dynamic_dbg.dir/matmul-transpose.cpp.obj
621.137 [29/6/298] Building CXX object flang-rt/unittests/Runtime/CMakeFiles/RuntimeTests.dir/CrashHandlerFixture.cpp.obj
622.397 [28/6/299] Building CXX object flang-rt/lib/runtime/CMakeFiles/flang_rt.runtime.dynamic_dbg.dir/findloc.cpp.obj
624.370 [27/6/300] Building CXX object flang-rt/unittests/Runtime/CMakeFiles/RuntimeTests.dir/Derived.cpp.obj
625.845 [26/6/301] Building CXX object flang-rt/unittests/Runtime/CMakeFiles/RuntimeTests.dir/Descriptor.cpp.obj
FAILED: [code=1] flang-rt/unittests/Runtime/CMakeFiles/RuntimeTests.dir/Descriptor.cpp.obj 
C:\buildbot\flang-x86_64-windows\build\bin\clang-cl.exe --target=x86_64-pc-windows-msvc  /nologo -TP -DGTEST_HAS_RTTI=0 -DGTEST_NO_LLVM_SUPPORT=1 -DLLVM_BUILD_STATIC -DNOT_EXE=\"C:/buildbot/flang-x86_64-windows/build/bin/not.exe\" -DUNICODE -D_CRT_NONSTDC_NO_DEPRECATE -D_CRT_NONSTDC_NO_WARNINGS -D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS -D_GLIBCXX_ASSERTIONS -D_GLIBCXX_NO_ASSERTIONS -D_HAS_EXCEPTIONS=0 -D_SCL_SECURE_NO_DEPRECATE -D_SCL_SECURE_NO_WARNINGS -D_UNICODE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -IC:/buildbot/flang-x86_64-windows/llvm-project/third-party/unittest/googletest/include -IC:/buildbot/flang-x86_64-windows/llvm-project/third-party/unittest/googlemock/include -IC:/buildbot/flang-x86_64-windows/llvm-project/flang-rt/include -IC:/buildbot/flang-x86_64-windows/llvm-project/flang-rt/../flang/include /DWIN32 /D_WINDOWS   /Zc:inline /Zc:__cplusplus /Oi /Brepro /bigobj /permissive- -Werror=unguarded-availability-new /W4 -wd4141 -wd4146 -wd4244 -wd4267 -wd4291 -wd4351 -wd4456 -wd4457 -wd4458 -wd4459 -wd4503 -wd4624 -wd4722 -wd4100 -wd4127 -wd4512 -wd4505 -wd4610 -wd4510 -wd4702 -wd4245 -wd4706 -wd4310 -wd4701 -wd4703 -wd4389 -wd4611 -wd4805 -wd4204 -wd4577 -wd4091 -wd4592 -wd4319 -wd4709 -wd5105 -wd4324 -wd4251 -wd4275 -w14062 -we4238 /Gw /O2 /Ob2  -std:c++17 -MD  /EHs-c- /GR- -UNDEBUG -Xclang --dependent-lib=C:/buildbot/flang-x86_64-windows/build/lib/clang/22/lib/windows/clang_rt.builtins-x86_64.lib /showIncludes /Foflang-rt/unittests/Runtime/CMakeFiles/RuntimeTests.dir/Descriptor.cpp.obj /Fdflang-rt\unittests\Runtime\CMakeFiles\RuntimeTests.dir\ -c -- C:/buildbot/flang-x86_64-windows/llvm-project/flang-rt/unittests/Runtime/Descriptor.cpp
C:/buildbot/flang-x86_64-windows/llvm-project/flang-rt/unittests/Runtime/Descriptor.cpp(188,5): error: use of undeclared identifier 'ftruncate'
  188 |     ftruncate(fileno(tmpf), 0);
      |     ^~~~~~~~~
1 error generated.
628.205 [26/5/302] Building CXX object flang-rt/unittests/Runtime/CMakeFiles/RuntimeTests.dir/InputExtensions.cpp.obj
629.361 [26/4/303] Building CXX object flang-rt/unittests/Runtime/CMakeFiles/RuntimeTests.dir/ExternalIOTest.cpp.obj
629.715 [26/3/304] Building CXX object flang-rt/unittests/Runtime/CMakeFiles/RuntimeTests.dir/Format.cpp.obj
631.266 [26/2/305] Building CXX object flang-rt/unittests/Runtime/CMakeFiles/RuntimeTests.dir/Inquiry.cpp.obj
653.335 [26/1/306] Building CXX object flang-rt/lib/runtime/CMakeFiles/flang_rt.runtime.dynamic_dbg.dir/matmul.cpp.obj
ninja: build stopped: subcommand failed.
FAILED: [code=1] runtimes/CMakeFiles/check-flang-rt C:/buildbot/flang-x86_64-windows/build/runtimes/CMakeFiles/check-flang-rt 
C:\Windows\system32\cmd.exe /C "cd /D C:\buildbot\flang-x86_64-windows\build\runtimes\runtimes-bins && "C:\Program Files\CMake\bin\cmake.exe" --build C:/buildbot/flang-x86_64-windows/build/runtimes/runtimes-bins/ --target check-flang-rt --config Release"
ninja: build stopped: subcommand failed.
Cache directory:    C:\Users\buildbot-worker\AppData\Local\ccache
Config file:        C:\Users\buildbot-worker\AppData\Local\ccache\ccache.conf
System config file: C:\ProgramData\ccache\ccache.conf
Stats updated:      12/01/25 18:38:14
Local storage:
  Cache size (GB):    5.0 / 5.0 (100.1%)
  Files:            22327
  Hits:                 0
  Misses:               0
  Reads:                0
  Writes:               0

@tstellar
Copy link
Collaborator

tstellar commented Dec 2, 2025

I'm seeing a build failure with the Windows release script that I think may be caused by this commit:
flang-rt/unittests/Runtime/Descriptor.cpp(188,5): error: use of undeclared identifier 'ftruncate'
Here is the full build log:
https://github.com/llvm/llvm-project/actions/runs/19842117405/job/56852610863

@Meinersbur
Copy link
Member

Meinersbur commented Dec 2, 2025

ftruncate does not exist on Windows, see https://stackoverflow.com/questions/19932243/is-there-a-function-in-windows-api-similar-to-ftruncate-on-posix

There are problems with ftruncate anyways, namely it interacts badly with buffering. I would suggest to close and reopen the file with O_TRUNC instead.

Meinersbur added a commit that referenced this pull request Dec 2, 2025
@Meinersbur
Copy link
Member

I reverted this because of the ongoing buildbot break. It already interfered with diagnosing #170142

@valerydmit
Copy link
Contributor Author

I reverted this because of the ongoing buildbot break. It already interfered with diagnosing #170142

Thanks for reverting this for me. I had a busy morning today so could not react quickly enough.

@llvm-ci
Copy link
Collaborator

llvm-ci commented Dec 2, 2025

LLVM Buildbot has detected a new failure on builder ppc64-flang-aix running on ppc64-flang-aix-test while building flang-rt,flang at step 7 "test-build-unified-tree-check-flang-rt".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/201/builds/7259

Here is the relevant piece of the build log for the reference
Step 7 (test-build-unified-tree-check-flang-rt) failure: test (failure)
******************** TEST 'flang-rt-Unit :: Runtime/./RuntimeTests/231/250' FAILED ********************
Script(shard):
--
GTEST_OUTPUT=json:/home/llvm/llvm-external-buildbots/workers/ppc64-flang-aix-test/ppc64-flang-aix-build/build/runtimes/runtimes-bins/flang-rt/unittests/Runtime/./RuntimeTests-flang-rt-Unit-15270646-231-250.json GTEST_SHUFFLE=0 GTEST_TOTAL_SHARDS=250 GTEST_SHARD_INDEX=231 /home/llvm/llvm-external-buildbots/workers/ppc64-flang-aix-test/ppc64-flang-aix-build/build/runtimes/runtimes-bins/flang-rt/unittests/Runtime/./RuntimeTests
--

Script:
--
/home/llvm/llvm-external-buildbots/workers/ppc64-flang-aix-test/ppc64-flang-aix-build/build/runtimes/runtimes-bins/flang-rt/unittests/Runtime/./RuntimeTests --gtest_filter=Descriptor.Dump
--
/home/llvm/llvm-external-buildbots/workers/ppc64-flang-aix-test/ppc64-flang-aix-build/llvm-project/flang-rt/unittests/Runtime/Descriptor.cpp:215: Failure
Expected equality of these values:
  output.c_str()
    Which is: "Descriptor @ ffffffffffff380:\n  base_addr fffffffffffeb80\n  elem_len  4\n  version   20240719\n  scalar\n  type      9 \"INTEGER(kind=4)\"\n  attribute 0\n  extra     0\n    addendum  0\n    alloc_idx 0\nDescriptor @ ffffffffffff380:\n  base_addr fffffffffffeb80\n  elem_len  4\n  version   20240719\n  scalar\n  type      9 \"CFI_type_int32_t\"\n  attribute 0\n  extra     0\n    addendum  0\n    alloc_idx 0\n"
  "Descriptor @ [address]:\n" "  base_addr [address]\n" "  elem_len  4\n" "  version   20240719\n" "  scalar\n" "  type      9 \"INTEGER(kind=4)\"\n" "  attribute 0\n" "  extra     0\n" "    addendum  0\n" "    alloc_idx 0\n" "Descriptor @ [address]:\n" "  base_addr [address]\n" "  elem_len  4\n" "  version   20240719\n" "  scalar\n" "  type      9 \"CFI_type_int32_t\"\n" "  attribute 0\n" "  extra     0\n" "    addendum  0\n" "    alloc_idx 0\n"
    Which is: "Descriptor @ [address]:\n  base_addr [address]\n  elem_len  4\n  version   20240719\n  scalar\n  type      9 \"INTEGER(kind=4)\"\n  attribute 0\n  extra     0\n    addendum  0\n    alloc_idx 0\nDescriptor @ [address]:\n  base_addr [address]\n  elem_len  4\n  version   20240719\n  scalar\n  type      9 \"CFI_type_int32_t\"\n  attribute 0\n  extra     0\n    addendum  0\n    alloc_idx 0\n"
With diff:
@@ -1,4 +1,4 @@
-Descriptor @ ffffffffffff380:
-  base_addr fffffffffffeb80
+Descriptor @ [address]:
+  base_addr [address]
   elem_len  4
   version   20240719
@@ -9,6 +9,6 @@
     addendum  0
     alloc_idx 0
-Descriptor @ ffffffffffff380:
-  base_addr fffffffffffeb80
+Descriptor @ [address]:
+  base_addr [address]
   elem_len  4
   version   20240719



/home/llvm/llvm-external-buildbots/workers/ppc64-flang-aix-test/ppc64-flang-aix-build/llvm-project/flang-rt/unittests/Runtime/Descriptor.cpp:215
Expected equality of these values:
  output.c_str()
    Which is: "Descriptor @ ffffffffffff380:\n  base_addr fffffffffffeb80\n  elem_len  4\n  version   20240719\n  scalar\n  type      9 \"INTEGER(kind=4)\"\n  attribute 0\n  extra     0\n    addendum  0\n    alloc_idx 0\nDescriptor @ ffffffffffff380:\n  base_addr fffffffffffeb80\n  elem_len  4\n  version   20240719\n  scalar\n  type      9 \"CFI_type_int32_t\"\n  attribute 0\n  extra     0\n    addendum  0\n    alloc_idx 0\n"
  "Descriptor @ [address]:\n" "  base_addr [address]\n" "  elem_len  4\n" "  version   20240719\n" "  scalar\n" "  type      9 \"INTEGER(kind=4)\"\n" "  attribute 0\n" "  extra     0\n" "    addendum  0\n" "    alloc_idx 0\n" "Descriptor @ [address]:\n" "  base_addr [address]\n" "  elem_len  4\n" "  version   20240719\n" "  scalar\n" "  type      9 \"CFI_type_int32_t\"\n" "  attribute 0\n" "  extra     0\n" "    addendum  0\n" "    alloc_idx 0\n"
    Which is: "Descriptor @ [address]:\n  base_addr [address]\n  elem_len  4\n  version   20240719\n  scalar\n  type      9 \"INTEGER(kind=4)\"\n  attribute 0\n  extra     0\n    addendum  0\n    alloc_idx 0\nDescriptor @ [address]:\n  base_addr [address]\n  elem_len  4\n  version   20240719\n  scalar\n  type      9 \"CFI_type_int32_t\"\n  attribute 0\n  extra     0\n    addendum  0\n    alloc_idx 0\n"
With diff:
@@ -1,4 +1,4 @@
-Descriptor @ ffffffffffff380:
-  base_addr fffffffffffeb80
+Descriptor @ [address]:
+  base_addr [address]
   elem_len  4
   version   20240719
...

valerydmit added a commit to valerydmit/llvm-project that referenced this pull request Dec 2, 2025
… extension

    Reapply  llvm#169137

    show_descriptor intrinsic prints details of a descriptor (extended
    Fortran pointer).
    It accepts a descriptor for any type and rank, including scalars.
    Requires use of flang_debug module.

    Example:
    program test
      use flang_debug
      implicit none
      integer :: a(4) = (/ 1,3,5,7 /)
      call show_descriptor(a(1:3))
    end program test

    and its output:
    Descriptor @ 0x7ffe01ec6a98:
      base_addr 0x563b7035103c
      elem_len  4
      version   20240719
      rank      1
      type      9 "INTEGER(kind=4)"
      attribute 0
      extra     0
        addendum  0
        alloc_idx 0
      dim[0] lower_bound 1
             extent      3
             sm          4
@jeanPerier
Copy link
Contributor

@Meinersbur, do you known if the GitHub CI bot is building flang-rt?

Looking at the GitHub checks on this PR, I see that the "Build and Test Windows" passed, and looking at the build log, I see that flang-rt is enabled, but I do not really see its files being actually built.

@Meinersbur
Copy link
Member

check-flang-rt has been explicitly disabled on Windows:

https://github.com/llvm/llvm-project/blob/main/.ci/compute_projects.py#L108

I do not know why

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants