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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -429,6 +430,15 @@ void InferAddressSpacesImpl::collectRewritableIntrinsicOperands(
appendsFlatAddressExpressionToPostorderStack(II->getArgOperand(0),
PostorderStack, Visited);
break;
case Intrinsic::is_constant: {
Value *Ptr = II->getArgOperand(0);
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


break;
}
case Intrinsic::masked_load:
case Intrinsic::masked_gather:
case Intrinsic::prefetch:
Expand Down
56 changes: 56 additions & 0 deletions llvm/test/Transforms/InferAddressSpaces/AMDGPU/is.constant.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
; 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
}

define i1 @is_constant_private_to_flat_v2(<2 x ptr addrspace(5)> %ptr) {
; CHECK-LABEL: define i1 @is_constant_private_to_flat_v2(
; CHECK-SAME: <2 x ptr addrspace(5)> [[PTR:%.*]]) {
; CHECK-NEXT: [[RET:%.*]] = call i1 @llvm.is.constant.v2p5(<2 x ptr addrspace(5)> [[PTR]])
; CHECK-NEXT: ret i1 [[RET]]
;
%cast = addrspacecast <2 x ptr addrspace(5)> %ptr to <2 x ptr>
%ret = call i1 @llvm.is.constant.v2p0(<2 x ptr> %cast)
ret i1 %ret
}

define i1 @is_constant_i32(i32 %val) {
; CHECK-LABEL: define i1 @is_constant_i32(
; CHECK-SAME: i32 [[VAL:%.*]]) {
; CHECK-NEXT: [[RET:%.*]] = call i1 @llvm.is.constant.i32(i32 [[VAL]])
; CHECK-NEXT: ret i1 [[RET]]
;
%ret = call i1 @llvm.is.constant.i32(i32 %val)
ret i1 %ret
}
Loading