Skip to content

Conversation

@sarnex
Copy link
Member

@sarnex sarnex commented Jun 6, 2025

There needs to be a space as the first character, otherwise the printed function prototype will have the CC attribute attached to the final ).

I noticed this looking at the AST for a function with __attribute__((device_kernel))

@sarnex sarnex changed the title [clang][AST] Fix spaces TypePrinter for some calling convs [clang][AST] Fix spaces in TypePrinter for some calling convs Jun 6, 2025
@sarnex sarnex marked this pull request as ready for review June 6, 2025 16:18
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jun 6, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 6, 2025

@llvm/pr-subscribers-clang

Author: Nick Sarnie (sarnex)

Changes

There needs to be a space as the first character, otherwise the printed function prototype will have the CC attribute attached to the final ).

I noticed this looking at the AST for a function with __attribute__((device_kernel))


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

1 Files Affected:

  • (modified) clang/lib/AST/TypePrinter.cpp (+3-3)
diff --git a/clang/lib/AST/TypePrinter.cpp b/clang/lib/AST/TypePrinter.cpp
index 330cfcd962825..d18723d807c6a 100644
--- a/clang/lib/AST/TypePrinter.cpp
+++ b/clang/lib/AST/TypePrinter.cpp
@@ -1095,13 +1095,13 @@ void TypePrinter::printFunctionAfter(const FunctionType::ExtInfo &Info,
       OS << " __attribute__((pcs(\"aapcs-vfp\")))";
       break;
     case CC_AArch64VectorCall:
-      OS << "__attribute__((aarch64_vector_pcs))";
+      OS << " __attribute__((aarch64_vector_pcs))";
       break;
     case CC_AArch64SVEPCS:
-      OS << "__attribute__((aarch64_sve_pcs))";
+      OS << " __attribute__((aarch64_sve_pcs))";
       break;
     case CC_DeviceKernel:
-      OS << "__attribute__((device_kernel))";
+      OS << " __attribute__((device_kernel))";
       break;
     case CC_IntelOclBicc:
       OS << " __attribute__((intel_ocl_bicc))";

Copy link
Contributor

@mizvekov mizvekov left a comment

Choose a reason for hiding this comment

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

Change looks good, but it would be better to include a regression test.

@sarnex
Copy link
Member Author

sarnex commented Jun 6, 2025

Thanks, will do.

Signed-off-by: Sarnie, Nick <[email protected]>
@sarnex
Copy link
Member Author

sarnex commented Jun 6, 2025

Just pushed a commit with a test, thanks!

@sarnex sarnex requested a review from mizvekov June 6, 2025 17:14
Comment on lines 11 to 24
#ifdef DEVICE
// CHECK-DEVICE-NOT: ()__attribute__((device_kernel))
void foo() __attribute__((device_kernel));
#endif

#ifdef VECTOR
// CHECK-VECTOR-NOT: ()__attribute__((aarch64_vector_pcs))
void foo() __attribute__((aarch64_vector_pcs));
#endif

#ifdef SVE
// CHECK-SVE-NOT: ()__attribute__((aarch64_sve_pcs))
void foo() __attribute__((aarch64_sve_pcs));
#endif
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 make this test a positive pattern, instead of a CHECK-NOT? Otherwise the test is a bit fragile.
This shouldn't be problem, since you already have strict-whitespace passed to FileCheck.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, with a positive pattern, it shouldn't be necessary to run the test three separate times, and you can remove the #ifdefs

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the review. I did the negative check because before this change the AST print is a little different for some reason where it contains the good string as part of the overall string, ex:

foo3 'void () __attribute__((aarch64_sve_pcs))':'void ()__attribute__((aarch64_sve_pcs))'

and after the fix the output is only

foo3 'void () __attribute__((aarch64_sve_pcs))'

so checking for foo3 'void () __attribute__((aarch64_sve_pcs))' doesn't work because it's a substring in the bad case.

but I managed to fix it by using regex and checking for EOL, it's not super pretty though, just pushed it, thanks.

Signed-off-by: Sarnie, Nick <[email protected]>
@sarnex sarnex requested a review from mizvekov June 6, 2025 18:23
Copy link
Contributor

@mizvekov mizvekov left a comment

Choose a reason for hiding this comment

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

Thanks, LGTM!

You might want to consider adding a change to the ReleaseNotes before merging this.

// RUN: %clang_cc1 -triple aarch64 -ast-dump -ast-dump-filter foo %s \
// RUN: | FileCheck --strict-whitespace %s

// CHECK: {{foo1 'void \(\) __attribute__\(\(device_kernel\)\)'$}}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// CHECK: {{foo1 'void \(\) __attribute__\(\(device_kernel\)\)'$}}
// CHECK: foo1 'void () __attribute__((device_kernel))'{{$}}

You don't need to apply the regex to the whole match, this makes it a little bit easier to read.

Copy link
Member Author

Choose a reason for hiding this comment

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

applied this change and added a release note, thanks so much!

sarnex added 2 commits June 6, 2025 16:07
Signed-off-by: Sarnie, Nick <[email protected]>
Signed-off-by: Sarnie, Nick <[email protected]>
@sarnex sarnex merged commit 56b9844 into llvm:main Jun 7, 2025
8 checks passed
tomtor pushed a commit to tomtor/llvm-project that referenced this pull request Jun 14, 2025
…43160)

There needs to be a space as the first character, otherwise the printed
function prototype will have the CC attribute attached to the final `)`.

I noticed this looking at the AST for a function with
`__attribute__((device_kernel))`

---------

Signed-off-by: Sarnie, Nick <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants