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

InstCombine adds incompatible attributes to call site #48154

Closed
ZequanWu opened this issue Jan 20, 2021 · 11 comments
Closed

InstCombine adds incompatible attributes to call site #48154

ZequanWu opened this issue Jan 20, 2021 · 11 comments
Labels
bugzilla Issues migrated from bugzilla

Comments

@ZequanWu
Copy link
Contributor

Bugzilla Link 48810
Resolution FIXED
Resolved on Jan 21, 2021 09:44
Version trunk
OS All
CC @aeubanks,@fhahn,@jdoerfert,@rotateright,@ZequanWu
Fixed by commit(s) 070af1b

Extended Description

Here is the minimum repro.
repro.c:
a, b;
void *mempcpy(void *, , long);
c(
) {
char *d = mempcpy(d, a, b);
*d = '\0';
c(d);
}

repro.sh:
clang -mllvm -attributor-enable=module -O2 repro.c

error:
Wrong types for attribute: inalloca nest noalias nocapture noundef nonnull readnone readonly signext zeroext byref(void) byval(void) preallocated(void) sret(void) align 1 dereferenceable(1) dereferenceable_or_null(1)
tail call nonnull dereferenceable(1) void @​llvm.memcpy.p0i8.p0i8.i64(i8* align 1 undef, i8* readonly align 1 %3, i64 %conv1, i1 false) #​3
in function c
fatal error: error in backend: Broken function found, compilation aborted!
clang-12: error: clang frontend command failed with exit code 70 (use -v to see invocation)

@jdoerfert
Copy link
Member

This is a bug, but not an Attributor one ;)

https://godbolt.org/z/vhsKPf

Looking where the attribute needs to be dropped now.

@jdoerfert
Copy link
Member

Somewhere in instcombine but I didn't find it immediately, I hope someone knows and gives an update tomorrow.

@ZequanWu
Copy link
Contributor Author

Thanks for pointing it out. I'll give it a try.

@jdoerfert
Copy link
Member

Thanks for pointing it out. I'll give it a try.

It might be as simple as this https://reviews.llvm.org/D87306

I haven't tried it though. We might not need to know where it comes from ;)

@rotateright
Copy link
Contributor

Minimal test:

define i32 @​#48810 (i32* %x) {
%r = call dereferenceable(1) i8* @​mempcpy(i8* undef, i32* null, i64 undef)
ret i32 undef
}

declare i8* @​mempcpy(i8*, i32*, i64)


Instcombine tries to convert that to a memcpy intrinsic:

Value *LibCallSimplifier::optimizeMemPCpy(CallInst *CI, IRBuilderBase &B) {
Value *Dst = CI->getArgOperand(0);
Value *N = CI->getArgOperand(2);
// mempcpy(x, y, n) -> llvm.memcpy(align 1 x, align 1 y, n), x + n
CallInst *NewCI =
B.CreateMemCpy(Dst, Align(1), CI->getArgOperand(1), Align(1), N);
NewCI->setAttributes(CI->getAttributes());


But IIUC, we can't apply "dereferenceable" to a void return.

@jdoerfert
Copy link
Member

Minimal test:

define i32 @​#48810 (i32* %x) {
%r = call dereferenceable(1) i8* @​mempcpy(i8* undef, i32* null, i64 undef)
ret i32 undef
}

declare i8* @​mempcpy(i8*, i32*, i64)


--

Instcombine tries to convert that to a memcpy intrinsic:

Value *LibCallSimplifier::optimizeMemPCpy(CallInst *CI, IRBuilderBase &B) {
Value *Dst = CI->getArgOperand(0);
Value *N = CI->getArgOperand(2);
// mempcpy(x, y, n) -> llvm.memcpy(align 1 x, align 1 y, n), x + n
CallInst *NewCI =
B.CreateMemCpy(Dst, Align(1), CI->getArgOperand(1), Align(1), N);
NewCI->setAttributes(CI->getAttributes());


--

But IIUC, we can't apply "dereferenceable" to a void return.

We need something like this (taken from InstCombineCalls.cpp:2281):

    // Get any return attributes.
    AttrBuilder RAttrs(CallerPAL, AttributeList::ReturnIndex);
    
    // If the return value is not being used, the type may not be compatible
    // with the existing attributes.  Wipe out any problematic attributes.
    RAttrs.remove(AttributeFuncs::typeIncompatible(NewRetTy));  

@rotateright
Copy link
Contributor

Thanks for the pointer!
Zequan, are you working on this? If not, I can post a patch for review.

@ZequanWu
Copy link
Contributor Author

No, I am not.
Feel free to post a patch. Thanks

@rotateright
Copy link
Contributor

@rotateright
Copy link
Contributor

Minimally fixed the crash:
https://reviews.llvm.org/rG070af1b7887f

@ZequanWu
Copy link
Contributor Author

Thanks for the quick fix.

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 11, 2021
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla
Projects
None yet
Development

No branches or pull requests

3 participants