Skip to content

InferAddressSpaces: Handle llvm.is.constant #102010

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

Merged
merged 1 commit into from
Aug 5, 2024

Conversation

arsenm
Copy link
Contributor

@arsenm arsenm commented Aug 5, 2024

No description provided.

Copy link
Contributor Author

arsenm commented Aug 5, 2024

@arsenm arsenm marked this pull request as ready for review August 5, 2024 16:28
@llvmbot
Copy link
Member

llvmbot commented Aug 5, 2024

@llvm/pr-subscribers-backend-amdgpu

@llvm/pr-subscribers-llvm-transforms

Author: Matt Arsenault (arsenm)

Changes

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

2 Files Affected:

  • (modified) llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp (+3-1)
  • (added) llvm/test/Transforms/InferAddressSpaces/AMDGPU/is.constant.ll (+35)
diff --git a/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp b/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
index 2ddf24be67702..67d4e7b5ee146 100644
--- a/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
+++ b/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
@@ -401,7 +401,8 @@ bool InferAddressSpacesImpl::rewriteIntrinsicOperands(IntrinsicInst *II,
     II->setCalledFunction(NewDecl);
     return true;
   }
-  case Intrinsic::prefetch: {
+  case Intrinsic::prefetch:
+  case Intrinsic::is_constant: {
     Function *NewDecl =
         Intrinsic::getDeclaration(M, II->getIntrinsicID(), {NewV->getType()});
     II->setArgOperand(0, NewV);
@@ -426,6 +427,7 @@ void InferAddressSpacesImpl::collectRewritableIntrinsicOperands(
   switch (IID) {
   case Intrinsic::ptrmask:
   case Intrinsic::objectsize:
+  case Intrinsic::is_constant:
     appendsFlatAddressExpressionToPostorderStack(II->getArgOperand(0),
                                                  PostorderStack, Visited);
     break;
diff --git a/llvm/test/Transforms/InferAddressSpaces/AMDGPU/is.constant.ll b/llvm/test/Transforms/InferAddressSpaces/AMDGPU/is.constant.ll
new file mode 100644
index 0000000000000..767681a281792
--- /dev/null
+++ b/llvm/test/Transforms/InferAddressSpaces/AMDGPU/is.constant.ll
@@ -0,0 +1,35 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S -mtriple=amdgcn-amd-amdhsa -passes=infer-address-spaces %s | FileCheck %s
+
+define i1 @is_constant_global_to_flat(ptr addrspace(1) %ptr) {
+; CHECK-LABEL: define i1 @is_constant_global_to_flat(
+; CHECK-SAME: ptr addrspace(1) [[PTR:%.*]]) {
+; CHECK-NEXT:    [[RET:%.*]] = call i1 @llvm.is.constant.p1(ptr addrspace(1) [[PTR]])
+; CHECK-NEXT:    ret i1 [[RET]]
+;
+  %cast = addrspacecast ptr addrspace(1) %ptr to ptr
+  %ret = call i1 @llvm.is.constant.p0(ptr %cast)
+  ret i1 %ret
+}
+
+define i1 @is_constant_local_to_flat(ptr addrspace(3) %ptr) {
+; CHECK-LABEL: define i1 @is_constant_local_to_flat(
+; CHECK-SAME: ptr addrspace(3) [[PTR:%.*]]) {
+; CHECK-NEXT:    [[RET:%.*]] = call i1 @llvm.is.constant.p3(ptr addrspace(3) [[PTR]])
+; CHECK-NEXT:    ret i1 [[RET]]
+;
+  %cast = addrspacecast ptr addrspace(3) %ptr to ptr
+  %ret = call i1 @llvm.is.constant.p0(ptr %cast)
+  ret i1 %ret
+}
+
+define i1 @is_constant_private_to_flat(ptr addrspace(5) %ptr) {
+; CHECK-LABEL: define i1 @is_constant_private_to_flat(
+; CHECK-SAME: ptr addrspace(5) [[PTR:%.*]]) {
+; CHECK-NEXT:    [[RET:%.*]] = call i1 @llvm.is.constant.p5(ptr addrspace(5) [[PTR]])
+; CHECK-NEXT:    ret i1 [[RET]]
+;
+  %cast = addrspacecast ptr addrspace(5) %ptr to ptr
+  %ret = call i1 @llvm.is.constant.p0(ptr %cast)
+  ret i1 %ret
+}

Copy link
Collaborator

@rampitec rampitec left a comment

Choose a reason for hiding this comment

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

Add some tests where argument is not a pointer?

@arsenm arsenm force-pushed the users/arsenm/infer-address-spaces-handle-is-constant branch from 2dde2ad to 227f60e Compare August 5, 2024 16:54
if (Ptr->getType()->isPtrOrPtrVectorTy()) {
appendsFlatAddressExpressionToPostorderStack(Ptr, PostorderStack,
Visited);
}
Copy link

Choose a reason for hiding this comment

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

unnecessary braces?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Multiple lines

Copy link

Choose a reason for hiding this comment

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

multiple lines is not listed as an excuse in the coding style

Copy link
Contributor Author

Choose a reason for hiding this comment

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

"However, braces should be used in cases where the omission of braces harm the readability and maintainability of the code."

and in my judgment this covers any situation with multiple lines

Copy link

Choose a reason for hiding this comment

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

How could it hurt readability?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because unlike a compiler I read by lines, and not statements.

The length of that section of the style guide is absurd. It should never be wrong to include braces. That's a lot simpler than parsing out whatever that is

Copy link
Member

Choose a reason for hiding this comment

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

It should never be wrong to include braces.

Having to deal with google style that demands braces everywhere, and LLVM which does not want them, my personal choice is "whatever the style guide says". It may not always be a perfect choice, but it's not worth anyone's time to argue over specific instances, where the right choice is ambiguous or is a matter of personal preference. I wish we could delegate braces/no-braces decisions to clang-format, too, but I don't think it currently handles that.

I'd stick with the style guide defaults and either have the braces removed, or a comment added to the body. Perhaps, making the function name shorter, and avoiding line-wrapping would address your readability concerns about braces/no-braces here, too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This rule is so open ended it's useless. If clang-format doesn't do this, it's not a rule. I'm not wasting more CI cycles to touch this

Copy link
Collaborator

@rampitec rampitec left a comment

Choose a reason for hiding this comment

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

LGTM modulo braces comment.

if (Ptr->getType()->isPtrOrPtrVectorTy()) {
appendsFlatAddressExpressionToPostorderStack(Ptr, PostorderStack,
Visited);
}
Copy link
Member

Choose a reason for hiding this comment

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

It should never be wrong to include braces.

Having to deal with google style that demands braces everywhere, and LLVM which does not want them, my personal choice is "whatever the style guide says". It may not always be a perfect choice, but it's not worth anyone's time to argue over specific instances, where the right choice is ambiguous or is a matter of personal preference. I wish we could delegate braces/no-braces decisions to clang-format, too, but I don't think it currently handles that.

I'd stick with the style guide defaults and either have the braces removed, or a comment added to the body. Perhaps, making the function name shorter, and avoiding line-wrapping would address your readability concerns about braces/no-braces here, too.

@tschuett
Copy link

tschuett commented Aug 5, 2024

From the clang-format documentation: https://clang.llvm.org/docs/ClangFormatStyleOptions.html#removebracesllvm

RemoveBracesLLVM (Boolean)

@Artem-B
Copy link
Member

Artem-B commented Aug 5, 2024

From the clang-format documentation: https://clang.llvm.org/docs/ClangFormatStyleOptions.html#removebracesllvm

Thank you for the pointer. Unfortunately, it looks like there's a good reason it does not seem to be on by default:

Setting this option to true could lead to incorrect code formatting due to clang-format’s lack of complete semantic information.

Copy link
Contributor Author

arsenm commented Aug 5, 2024

Merge activity

  • Aug 5, 4:10 PM EDT: @arsenm started a stack merge that includes this pull request via Graphite.
  • Aug 5, 4:17 PM EDT: Graphite rebased this pull request as part of a merge.
  • Aug 5, 4:20 PM EDT: @arsenm merged this pull request with Graphite.

@arsenm arsenm force-pushed the users/arsenm/infer-address-spaces-masked-load-store branch from 0cd0fd7 to fd7b3c2 Compare August 5, 2024 20:14
Base automatically changed from users/arsenm/infer-address-spaces-masked-load-store to main August 5, 2024 20:17
@arsenm arsenm force-pushed the users/arsenm/infer-address-spaces-handle-is-constant branch from 227f60e to 71a166f Compare August 5, 2024 20:17
@arsenm arsenm merged commit 2ef553c into main Aug 5, 2024
3 of 5 checks passed
@arsenm arsenm deleted the users/arsenm/infer-address-spaces-handle-is-constant branch August 5, 2024 20:20
@tschuett
Copy link

tschuett commented Aug 6, 2024

Braces still there and no response to @Artem-B? I don't like that.

@arsenm
Copy link
Contributor Author

arsenm commented Aug 6, 2024

Braces still there and no response to @Artem-B? I don't like that.

I posted the reply yesterday but graphite apparently didn't submit it

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

Successfully merging this pull request may close these issues.

5 participants