[RFC] __builtin_assume, [[assume]] optimization behavior

You and I have the same opinion here I think :slight_smile:

I don’t want to touch __builtin_assume with the flag, if someone wants to disable that, it should perhaps be a separate flag. As far as the above pattern: I’m kind of the mind of, ā€œdisabling this is effectively getting you back to pre-attribute behavior, so if the programmer REALLY wanted to assume, the feature-test-macro check is their way out (backing up to the builtin)ā€.

I think the ā€˜default’ should be the result of the #1 vs #3 vote above, and it looks like #3 is winning, so I’d expect ā€˜assume’ to be default.

1 Like

Thank you everyone for the good discussion around this RFC! Now that discussion seems to have wound down, I think we’ve got consensus on a way forward.

  • Do not change the behavior of __builtin_assume
  • Support [[assume]] by lowering its operand to LLVM IR
  • Add an on/off driver flag allowing the user to enable or disable the [[assume]] attribute as they need
    • Enabled by default in C++23 mode

The rest of the details can be hammered out during review regarding the naming of the driver flags, whether we want to support the attribute in older standards modes or C, etc.

7 Likes

It would also be good to just add checking of the assumption to ubsan (but only if it has no side effects). It definitely fits the kinds of things ubsan is designed to catch.

1 Like

Yup, integration with UBSan is a good idea, but I don’t think it’s a requirement for the initial implementation, right?

Yeah, I don’t think it should block the initial commits. Just wanted to make sure that was consensus on the resolution to how to check them and didn’t see it in the list, but if that was just for the initial work then that’s fine.

1 Like

Yep, rather than the discussion about whether enforce is a dialect/etc - +1 to enforcement via ubsan being consistent with other undefined behavior checking.

(has anyone written up more detail on what assumptions tend to risk negative perf consequences? I’d have thought it was basically anything non-transparent to the compiler? Like we don’t have any way to mark the contents of the assumption has side-effect-agnostic, right? So if you just put an arbitrary function call in your assumption, LLVM doesn’t know it can drop it, so preserves it etc - even some fairly broad description like that seems plausible to document, again, for the kinds of power users reaching for this sort of tool anyway - leaving folks hopefully adding more direct assumptions like ā€œthis thing is non-null, non-zero, less than 47, etcā€ which I’d expect to be pretty benign/helpful.
Could even go so far as to warn if an external function call is made from an assumption - but that’d only catch the superficial cases, not the case of calling an inline function that then calls an external function, etc… )

Hmm, maybe I’m misunderstanding something here, but if the assumption has side-effects at the C++ level, then we currently just don’t emit anything at all for it in Clang codegen (and issue a warning that the expression has side-effects) for __builtin_assume (and also [[assume]] in the current implementation).

Ah, so it does - even an inline function call gets the warning and is discarded/not emitted into the LLVM IR.

I was just making a rough guess - hadn’t checked the implementation in a while - so not sure what might be the common idioms that lead to inefficiencies in code generation due to assumptions at this point…

The initial [[asume]] PR was merged today.

I was wondering if improvement to the llvm assume intrinsic could potentially make for a good GSoC project as it seems that no one has time to work on that otherwise?

2 Likes

I believe so. I’m happy to help (mentor). We would hopefully get the data we need to make more informed decisions on how to proceed.

3 Likes

Would you be amenable to updating The LLVM Compiler Infrastructure Project and moving it up to the 2024 set of open projects we’re considering? Maybe that will help entice a student to apply?

This would be wonderful. I’d be happy to help in any way I can (Sadly, anything past the clang front-end could as well be black magic to me so I probably would not be able to help much)