Skip to content
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

[Flang] Intrinsic LEN and C_SIZEOF returns incorrect result when the argument is a CFI_CDESC_T descriptor of character type. #92421

Closed
DanielCChen opened this issue May 16, 2024 · 3 comments
Labels
flang:runtime question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

Comments

@DanielCChen
Copy link
Contributor

Consider the following code:

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "ISO_Fortran_binding.h"
void sub(CFI_cdesc_t* a);
int main()
{
    int rc;
    char *p = "12345";
    CFI_cdesc_t* a;
    CFI_CDESC_T(1) at1;
    a = (CFI_cdesc_t *) &at1;

    rc = CFI_establish(a, p, CFI_attribute_pointer, CFI_type_signed_char, 5, 0, 0);
    assert(rc == CFI_SUCCESS);
    sub(a);
}

Fortran subroutine sub is as:

      subroutine sub(c_arg2) bind(c)
        use, intrinsic :: iso_c_binding
        character(*) :: c_arg2!(*)
        print*, "LEN(c_arg2)=", LEN(c_arg2)
        print*, "c_sizeof(c_arg2)=", c_sizeof(c_arg2)
      end

The expected result is

 LEN(c_arg2)= 5
 c_sizeof(c_arg2)= 5

But Flang currently outputs

 LEN(c_arg2)= 1
 c_sizeof(c_arg2)= 1
@github-actions github-actions bot added the flang Flang issues not falling into any other category label May 16, 2024
@jeanPerier
Copy link
Contributor

jeanPerier commented May 16, 2024

@DanielCChen, is there a reason the code is using CFI_type_signed_char and not CFI_type_char?

The problem is that CFI_type_signed_char is not recognized as being a character type in CFI_establish, that ends-up overriding the elem_len argument (5) with1.

!IsCharacterType(type)) {

static inline constexpr RT_API_ATTRS bool IsCharacterType(CFI_type_t ty) {

I think flang is doing a correct interpretation given that CFI_type_signed_char is considered to be an integer type in in table 18.2/18.4. CFI_type_char should be used instead. The CFI_establish requirements in F2023 18.5.5.5 requires elem_len to be ignored if type is nor CFI_type_struct, nor CFI_type_other, nor a Fortran character type code.

@DanielCChen
Copy link
Contributor Author

@jeanPerier Thanks for the comment. You are right. c_sighed_char is indeed listed as Fortran integer type.

@EugeneZelenko EugeneZelenko added question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead! flang:runtime and removed flang Flang issues not falling into any other category labels May 17, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented May 17, 2024

@llvm/issue-subscribers-flang-runtime

Author: Daniel Chen (DanielCChen)

Consider the following code: ``` #include <stdio.h> #include <string.h> #include <assert.h> #include "ISO_Fortran_binding.h" void sub(CFI_cdesc_t* a); int main() { int rc; char *p = "12345"; CFI_cdesc_t* a; CFI_CDESC_T(1) at1; a = (CFI_cdesc_t *) &at1;
rc = CFI_establish(a, p, CFI_attribute_pointer, CFI_type_signed_char, 5, 0, 0);
assert(rc == CFI_SUCCESS);
sub(a);

}


Fortran subroutine `sub` is as:
  subroutine sub(c_arg2) bind(c)
    use, intrinsic :: iso_c_binding
    character(*) :: c_arg2!(*)
    print*, "LEN(c_arg2)=", LEN(c_arg2)
    print*, "c_sizeof(c_arg2)=", c_sizeof(c_arg2)
  end

The expected result is

LEN(c_arg2)= 5
c_sizeof(c_arg2)= 5


But Flang currently outputs

LEN(c_arg2)= 1
c_sizeof(c_arg2)= 1

</details>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:runtime question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!
Projects
None yet
Development

No branches or pull requests

4 participants