This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Remove early constant fold
ClosedPublic

Authored by nikic on Feb 20 2023, 2:03 AM.

Details

Summary

InstCombine currently performs a constant folding attempt as part of the main InstCombine loop, before visiting the instruction. However, each visit method will also attempt to simplify the instruction, which will in turn constant fold it. (Additionally, we also constant fold instructions before the main InstCombine loop and use a constant folding IR builder, so this is doubly redundant.)

There is one place where InstCombine visit methods currently don't call into simplification, and that's casts. To be conservative, I've added an explicit constant folding call there (though it has no impact on tests).

This makes for a mild compile-time improvement (http://llvm-compile-time-tracker.com/compare.php?from=b15d70e57a440c37c11b0ad3173d7dc624e4fa07&to=1753e2859139af9a8a8551780c791e53ad20f90d&stat=instructions:u) and in particular mitigates the compile-time regression from enabling load simplification in https://github.com/llvm/llvm-project/commit/be88b5814d9efce131dbc0c8e288907e2e6c89be.

Diff Detail

Event Timeline

nikic created this revision.Feb 20 2023, 2:03 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 20 2023, 2:03 AM
nikic requested review of this revision.Feb 20 2023, 2:03 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 20 2023, 2:03 AM
nikic added inline comments.Feb 20 2023, 2:05 AM
llvm/test/Transforms/InstCombine/pr38677.ll
15

The reason for this change is that select constant folding in InstSimplify will only return if the select actually folds, not if it would create a constant expression. Arguably this is a bug in InstSimplify, but I decided not to fix it, since we don't actually want these select constant expressions.

nikic edited the summary of this revision. (Show Details)Feb 20 2023, 2:05 AM
spatel accepted this revision.Feb 20 2023, 5:25 AM

LGTM - thanks!

This revision is now accepted and ready to land.Feb 20 2023, 5:25 AM
This revision was landed with ongoing or failed builds.Feb 20 2023, 7:49 AM
This revision was automatically updated to reflect the committed changes.
bgraur added a subscriber: bgraur.Feb 24 2023, 9:32 AM
vitalybuka reopened this revision.EditedFeb 24 2023, 10:12 AM
vitalybuka added a subscriber: vitalybuka.

I am going to revert this one. Huge regression with ubsan or particular files.
I'll post reproducer here.

Flaky timeout on the bot https://lab.llvm.org/buildbot/#/builders/238 is from this.
But no need to look into bot yet, I believe I have -E reproducer.

This revision is now accepted and ready to land.Feb 24 2023, 10:12 AM

Original is on ARM https://gist.github.com/vitalybuka/82f6c5b2ab51ee027df13c2c35a95ffb

but I can reproduce compiling the one above on my default x86_64 checkout:

time <yourbuild>/bin/clang++ -fsanitize=undefined -Wl,--rpath=/b/sanitizer-aarch64-linux-bootstrap-ubsan/build/libcxx_build_ubsan/lib -L/b/sanitizer-aarch64-linux-bootstrap-ubsan/build/libcxx_build_ubsan/lib -w -stdlib=libc++ -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -pedantic -Wno-long-long -Wc++98-compat-extra-semi -Wimplicit-fallthrough -Wcovered-switch-default -Wno-class-memaccess -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -Wmisleading-indentation -Wctad-maybe-unsupported -fno-omit-frame-pointer -gline-tables-only -fsanitize=undefined -fno-sanitize=vptr,function -fno-sanitize-recover=all -fdiagnostics-color -ffunction-sections -fdata-sections -fno-common -Woverloaded-virtual -Wno-nested-anon-types -O3 -DNDEBUG -Wno-variadic-macros -Wno-gnu-zero-variadic-macro-arguments -fno-exceptions -fno-rtti -Wno-suggest-override -std=c++17 -o /dev/null -c t.cpp

after revert:
real 3m51.948s
user 3m50.172s
sys 0m1.756s

before revert:
real 16m18.127s
user 16m16.255s
sys 0m1.801s

it would be nice to compile clang itself as benchmark, but I guess it's significantly more load for tracker

it would be nice to compile clang itself as benchmark, but I guess it's significantly more load for tracker

Could you just time the bootstrap tests and if they have an extreme regression fail the test?

This revision was automatically updated to reflect the committed changes.
vitalybuka added a comment.EditedFeb 27 2023, 12:53 PM

Could you just time the bootstrap tests and if they have an extreme regression fail the test?

Thanks, looks just "time" is good enough. User time is quite consistent. Original version of the patch increased time ~20%, reapply is close to 0%
https://lab.llvm.org/buildbot/#/builders/238/builds/1576