This is an archive of the discontinued LLVM Phabricator instance.

[DeadStoreElimination] Handle null accessing
AbandonedPublic

Authored by ChuanqiXu on Jul 20 2022, 2:00 AM.

Details

Summary

After we landed https://reviews.llvm.org/D127383, the compiler may crash due to it tries to access a may-null value. This is because https://github.com/llvm/llvm-project/blob/051738b08cf5e39fd274dd379147d1c19e2b5b20/llvm/lib/Analysis/MemoryLocation.cpp#L121-L122 may return null after we land D127383.

This should be a noop before D127383 landed.

Diff Detail

Event Timeline

ChuanqiXu created this revision.Jul 20 2022, 2:00 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 20 2022, 2:00 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
ChuanqiXu requested review of this revision.Jul 20 2022, 2:00 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 20 2022, 2:00 AM
ChuanqiXu retitled this revision from [DRAFT] [DeadStoreElimination] Handle null accessing to [DeadStoreElimination] Handle null accessing.Jul 20 2022, 2:49 AM
ChuanqiXu added a reviewer: nikic.
ChuanqiXu retitled this revision from [DeadStoreElimination] Handle null accessing to [NFC] [DeadStoreElimination] Handle null accessing.Jul 20 2022, 2:52 AM
ChuanqiXu edited the summary of this revision. (Show Details)

@nikic Are you happy to land this and D130153? Sorry I'm hurry since that I want to handle TLS problem in coroutines in clang15 and clang15 is going to be branched in 7.26

ChuanqiXu retitled this revision from [NFC] [DeadStoreElimination] Handle null accessing to [DeadStoreElimination] Handle null accessing.
ChuanqiXu edited the summary of this revision. (Show Details)

Add tests

llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
1927

Since there is a comment. I am not sure if this is a good fix.

I think both this change and D130153 are fine in principle, but I am concerned about the larger issue this exposes. Code has been making (historically quite reasonable) assumptions about how certain intrinsics work, for example that debug intrinsics don't access memory. These assumptions are still correct, but LLVM doesn't know this anymore, because it thinks that debug intrinsics, or memset, etc can read the thread ID now (or in this case, it's one step further, in that we lose the information that memset can only write to its argument, because we don't track complex scenarios like "can only write argument memory, but read other memory").

I guess if the number of places that make assumptions are limited, it may be okay to add such workarounds, but it's rather unfortunate. I also suspect that this can manifest in less obvious ways, e.g. because debug invariance in some places may depend on debug intrinsics not accessing memory.

I think both this change and D130153 are fine in principle, but I am concerned about the larger issue this exposes. Code has been making (historically quite reasonable) assumptions about how certain intrinsics work, for example that debug intrinsics don't access memory. These assumptions are still correct, but LLVM doesn't know this anymore, because it thinks that debug intrinsics, or memset, etc can read the thread ID now (or in this case, it's one step further, in that we lose the information that memset can only write to its argument, because we don't track complex scenarios like "can only write argument memory, but read other memory").

I guess if the number of places that make assumptions are limited, it may be okay to add such workarounds, but it's rather unfortunate. I also suspect that this can manifest in less obvious ways, e.g. because debug invariance in some places may depend on debug intrinsics not accessing memory.

The key point here is about the numbers of the assumptions. And if we break an assumption, we should meet a compilation crash immediately. So I think the scope of the problem could be tested. I've tested our internal workloads and folly (The largest open sourced user of coroutines I know). So it looks like the number is limited from the experiment.

@nikic would you mind landing this? Since 15.x branch is going to be branched and the thread identification problem is really important to users of coroutines, I want to have a workaround for it. And there is more than one month long testing time so that we could revert it if we find anything bad.

nikic added a comment.Jul 26 2022, 2:38 AM

@nikic would you mind landing this? Since 15.x branch is going to be branched and the thread identification problem is really important to users of coroutines, I want to have a workaround for it. And there is more than one month long testing time so that we could revert it if we find anything bad.

Would it be "good enough" to mark coro.tls.wrapper as reading memory for LLVM 15, if we're adding a temporary intrinsic anyway? Or is it very important that __attribute__((const)) functions also get "fixed"?

@nikic would you mind landing this? Since 15.x branch is going to be branched and the thread identification problem is really important to users of coroutines, I want to have a workaround for it. And there is more than one month long testing time so that we could revert it if we find anything bad.

Would it be "good enough" to mark coro.tls.wrapper as reading memory for LLVM 15, if we're adding a temporary intrinsic anyway?

Do you mean to mark coro.tls.wrapper as readonly?

Or is it very important that __attribute__((const)) functions also get "fixed"?

Yeah, it is important. Many users need to call pthread_self() or getid() to get the current the thread.

And this should be fixed/workarounded in D127383. Do you mean to not land D130142 in 15.x? From my understanding, if we're concerning it breaks some assumptions, it should be easy to test. And if we're concerning about the impact to the IR model, it depends on whether we agree the consensus we made in previous discussions.

@nikic I guess we could try to land this one since 15.x is branched and we have enough time to test.

ChuanqiXu abandoned this revision.Dec 15 2022, 8:50 PM