This is an archive of the discontinued LLVM Phabricator instance.

Remove the NotUnderValgrind caching flag
ClosedPublic

Authored by mehdi_amini on Jul 16 2021, 6:21 PM.

Details

Summary

The motivation for this caching wasn't clear, remove it in an effort to
simplify the code and make libSupport free of global dynamic constructor.

Diff Detail

Event Timeline

mehdi_amini created this revision.Jul 16 2021, 6:21 PM
mehdi_amini requested review of this revision.Jul 16 2021, 6:21 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 16 2021, 6:21 PM
MaskRay added a comment.EditedJul 16 2021, 7:15 PM

Perhaps we should just get rid of the file. It (2010) seems to be related to the old JIT lib/Target/X86/X86JITInfo.cpp which was deleted in 2014. The file has another usage in bugpoint.cpp to tune a parameter which appears not to useful.

I have the impression that it can be called transitively from Orc as well, through calls to protectMappedMemory

This code predates my involvement in the JIT by half a decade or more. ;)

It's definitely reachable, but I'm not sure whether anyone actually relies on it any more. My first impression is that running Valgrind on JIT'd code would be pretty rare these days. Maybe we just remove it after we branch for LLVM 13 in a week or so?

vchuravy added a project: Restricted Project.Jul 17 2021, 5:47 AM

@loladiro does this impact us? Julia does support running under valgrind, see https://docs.julialang.org/en/v1/devdocs/valgrind/,
and IIRC that extends to JIT'ed code.

There doesn't seem to be a reason to make the VALGRIND_DISCARD_TRANSLATIONS call conditional. The macro is already a specific sequence of bytes (e.g. rol instructions) that are a no-op if valgrind is not running the particular segment of code inside its own JIT

dexonsmith added inline comments.Jul 19 2021, 11:37 AM
llvm/lib/Support/Valgrind.cpp
21–24

Can NotUnderValgrind be merged into RunningOnValgrind, skipping the managed static?

The only reason not to is if it RUNNING_ON_VALGRIND could return "true" on the first use, but "false" some time later in the same execution. I don't imagine that's possible...?

... but if it is possible, maybe switching to ManagedStatic isn't safe either, since there would be a reason to cache the value at load-time instead of at first use.

26–27

Seems to me like this could use RunningOnValgrind, unless there's a case where RUNNING_ON_VALGRIND returns different answers at load-time vs later...

dexonsmith added inline comments.Jul 19 2021, 11:41 AM
llvm/lib/Support/Valgrind.cpp
21–24

(Based on https://www.valgrind.org/docs/manual/manual-core-adv.html I strongly doubt load-time is special in any way...)

mehdi_amini added inline comments.Jul 19 2021, 4:42 PM
llvm/lib/Support/Valgrind.cpp
21–24

Yeah I'm confused by all this logic, and I didn't get the comment either. I don't get why we don't just return RUNNING_ON_VALGRIND and forget about caching entirely?

There doesn't seem to be a reason to make the VALGRIND_DISCARD_TRANSLATIONS call conditional. The macro is already a specific sequence of bytes (e.g. rol instructions) that are a no-op if valgrind is not running the particular segment of code inside its own JIT

Right, I'm happy to simplify all of this code and just rely on the Valgrind macros. It's a case of Chesterton's fence really: the code was added as-is >10 years ago and it isn't clear what behavior is relied on.

dexonsmith added inline comments.Jul 19 2021, 4:46 PM
llvm/lib/Support/Valgrind.cpp
21–24

Maybe there was a time when it wasn't fast? Might be worth a quick look at git-blame to see if this was in response to a specific performance problem. Might be better to remove the caching entirely.

26–27

If the caching is removed it's probably worth following @vtjnash's suggestion to remove the explicit check here (since it's apparently redundant).

dexonsmith added inline comments.Jul 19 2021, 5:04 PM
llvm/lib/Support/Valgrind.cpp
21–24

Based on the commit message, I'd bet the author just assumed it was slow and needed caching. Maybe it even did at the time, but the current valgrind docs imply that it's fast (six instructions IIUC). I'd be in favour of calling the macros directly without caching.

Nuking these methods and running the LLVM tests with Valgrind still worked: bin/llvm-lit ../llvm/test/ --vg

Maybe we can delete this entirely? Can one of the Julia folks look if you need this (and provide a test)?

mehdi_amini retitled this revision from Lazy initialized the NotUnderValgrind flag to Remove the NotUnderValgrind caching flag.
mehdi_amini edited the summary of this revision. (Show Details)

Remove the "caching" flag entirely for now

Remove unused include now

dexonsmith accepted this revision.Jul 20 2021, 2:30 PM

Updated patch LGTM, with a couple of nits inline.

(Agreed that it'd be great for Julia to write a test for the LLVM JIT that confirms it behaves correctly under Valgrind.)

llvm/lib/Support/Valgrind.cpp
17

You can drop this now.

22–23

Probably clang-format will want to delete some newlines here.

This revision is now accepted and ready to land.Jul 20 2021, 2:30 PM
mehdi_amini added inline comments.Jul 20 2021, 3:17 PM
llvm/lib/Support/Valgrind.cpp
22–23

I have a clang-format running automatically on arc diff ;)

I think the lines appear empty here because of the removal on the left hand side, but if you look at the line numbers there aren't any blank line I think?

dexonsmith added inline comments.Jul 20 2021, 3:20 PM
llvm/lib/Support/Valgrind.cpp
22–23

Aha, yeah, that explains it :).

This revision was landed with ongoing or failed builds.Jul 25 2021, 5:25 PM
This revision was automatically updated to reflect the committed changes.