This is an archive of the discontinued LLVM Phabricator instance.

[SCCP] Use constant ranges for casts.
ClosedPublic

Authored by fhahn on Dec 27 2019, 8:37 AM.

Details

Summary

For casts with constant range operands, we can use
ConstantRange::castOp.

Diff Detail

Event Timeline

fhahn created this revision.Dec 27 2019, 8:37 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 27 2019, 8:37 AM
Herald added a subscriber: hiraditya. · View Herald Transcript

Unit tests: fail. 61132 tests passed, 1 failed and 728 were skipped.

failed: Clang.SemaOpenCL/numbered-address-space.cl

clang-tidy: fail. Please fix clang-tidy findings.

clang-format: pass.

Build artifacts: diff.json, clang-tidy.txt, clang-format.patch, CMakeCache.txt, console-log.txt, test-results.xml

efriedma added inline comments.Jan 20 2020, 5:26 PM
llvm/lib/Transforms/Scalar/SCCP.cpp
813

DL.getTypeSizeInBits(DestTy)?

fhahn updated this revision to Diff 239754.Jan 22 2020, 6:05 PM
fhahn marked an inline comment as done.

Use DL.getTypeSizeInBits.

Unit tests: unknown.

clang-tidy: unknown.

clang-format: unknown.

Build artifacts: diff.json, console-log.txt

fhahn updated this revision to Diff 245727.Feb 20 2020, 1:02 PM

Only cast between integer types with constant ranges.

I am somehow missing the extra folding in the test case. What is folded now?

fhahn added a comment.Feb 20 2020, 1:34 PM

I am somehow missing the extra folding in the test case. What is folded now?

Nothing, the test make sure we skip int -> fp casts, rather than crash :)

I am somehow missing the extra folding in the test case. What is folded now?

Nothing, the test make sure we skip int -> fp casts, rather than crash :)

Gotcha.

fhahn updated this revision to Diff 250919.Mar 17 2020, 2:59 PM

Ping. I think all required patches should have landed over the last week or so.

Rebased.

efriedma added inline comments.Mar 27 2020, 3:45 PM
llvm/lib/Transforms/Scalar/SCCP.cpp
807

This is calling both markConstant and mergeInValue?

809

If I'm following correctly, both the source and destination must be integers, so this is one of bitcast/trunc/sext/zext?

fhahn updated this revision to Diff 253427.Mar 29 2020, 9:23 AM
fhahn marked 4 inline comments as done.

Thanks Eli!

Removed the unnecessary mergeIn.

llvm/lib/Transforms/Scalar/SCCP.cpp
807

This could just be markConstant as it was originally I think. Not sure if there are any scenarios where we would get constant after already having a (non-singleton) range.

809

Yep, otherwise we would mark the result of %tmp4 = sitofp i32 %r to double as constant range, if %r is a constant range.

This revision is now accepted and ready to land.Mar 30 2020, 3:05 PM
This revision was automatically updated to reflect the committed changes.

I'm now getting an assertion failure in the LNT test suite:
http://lab.llvm.org:8011/builders/clang-s390x-linux-lnt/builds/17826/steps/test-suite/logs/stdio

clang: /home/uweigand/sandbox/buildbot/clang-s390x-linux-lnt/llvm/llvm/lib/IR/Constants.cpp:1661: static llvm::Constant* llvm::ConstantExpr::getCast(unsigned int, llvm::Constant*, llvm::Type*, bool): Assertion `CastInst::castIsValid(opc, C, Ty) && "Invalid constantexpr cast!"' failed.
PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0. Program arguments: /home/uweigand/sandbox/buildbot/clang-s390x-linux-lnt/stage1.install/bin/clang -DNDEBUG -O3 -DNDEBUG -w -Werror=date-time -w -o CMakeFiles/GCC-C-execute-scal-to-vec1.dir/scal-to-vec1.c.o -c /home/uweigand/sandbox/buildbot/clang-s390x-linux-lnt/test/test-suite/SingleSource/Regression/C/gcc-c-torture/execute/scal-to-vec1.c

  1. <eof> parser at end of file
  2. Per-module optimization passes
  3. Running pass 'CallGraph Pass Manager' on module '/home/uweigand/sandbox/buildbot/clang-s390x-linux-lnt/test/test-suite/SingleSource/Regression/C/gcc-c-torture/execute/scal-to-vec1.c'.
  4. Running pass 'Sparse Conditional Constant Propagation' on function '@main'

[...]
#9 0x000002aa1a5b6b70 llvm::ConstantExpr::getCast(unsigned int, llvm::Constant*, llvm::Type*, bool) (/home/uweigand/sandbox/buildbot/clang-s390x-linux-lnt/stage1.install/bin/clang+0x2ab6b70)
#10 0x000002aa19ec233c llvm::ConstantFoldCastOperand(unsigned int, llvm::Constant*, llvm::Type*, llvm::DataLayout const&) (/home/uweigand/sandbox/buildbot/clang-s390x-linux-lnt/stage1.install/bin/clang+0x23c233c)
#11 0x000002aa1ac69ab0 (anonymous namespace)::SCCPSolver::visitCastInst(llvm::CastInst&) (/home/uweigand/sandbox/buildbot/clang-s390x-linux-lnt/stage1.install/bin/clang+0x3169ab0)
#12 0x000002aa1ac6ce72 llvm::InstVisitor<(anonymous namespace)::SCCPSolver, void>::visit(llvm::Instruction&) (/home/uweigand/sandbox/buildbot/clang-s390x-linux-lnt/stage1.install/bin/clang+0x316ce72)
[...]

that appears to be directly related to this visitCastInst change.

fhahn added a comment.Mar 31 2020, 8:53 AM

I'm now getting an assertion failure in the LNT test suite:
http://lab.llvm.org:8011/builders/clang-s390x-linux-lnt/builds/17826/steps/test-suite/logs/stdio

clang: /home/uweigand/sandbox/buildbot/clang-s390x-linux-lnt/llvm/llvm/lib/IR/Constants.cpp:1661: static llvm::Constant* llvm::ConstantExpr::getCast(unsigned int, llvm::Constant*, llvm::Type*, bool): Assertion `CastInst::castIsValid(opc, C, Ty) && "Invalid constantexpr cast!"' failed.
PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0. Program arguments: /home/uweigand/sandbox/buildbot/clang-s390x-linux-lnt/stage1.install/bin/clang -DNDEBUG -O3 -DNDEBUG -w -Werror=date-time -w -o CMakeFiles/GCC-C-execute-scal-to-vec1.dir/scal-to-vec1.c.o -c /home/uweigand/sandbox/buildbot/clang-s390x-linux-lnt/test/test-suite/SingleSource/Regression/C/gcc-c-torture/execute/scal-to-vec1.c

  1. <eof> parser at end of file
  2. Per-module optimization passes
  3. Running pass 'CallGraph Pass Manager' on module '/home/uweigand/sandbox/buildbot/clang-s390x-linux-lnt/test/test-suite/SingleSource/Regression/C/gcc-c-torture/execute/scal-to-vec1.c'.
  4. Running pass 'Sparse Conditional Constant Propagation' on function '@main'

[...]
#9 0x000002aa1a5b6b70 llvm::ConstantExpr::getCast(unsigned int, llvm::Constant*, llvm::Type*, bool) (/home/uweigand/sandbox/buildbot/clang-s390x-linux-lnt/stage1.install/bin/clang+0x2ab6b70)
#10 0x000002aa19ec233c llvm::ConstantFoldCastOperand(unsigned int, llvm::Constant*, llvm::Type*, llvm::DataLayout const&) (/home/uweigand/sandbox/buildbot/clang-s390x-linux-lnt/stage1.install/bin/clang+0x23c233c)
#11 0x000002aa1ac69ab0 (anonymous namespace)::SCCPSolver::visitCastInst(llvm::CastInst&) (/home/uweigand/sandbox/buildbot/clang-s390x-linux-lnt/stage1.install/bin/clang+0x3169ab0)
#12 0x000002aa1ac6ce72 llvm::InstVisitor<(anonymous namespace)::SCCPSolver, void>::visit(llvm::Instruction&) (/home/uweigand/sandbox/buildbot/clang-s390x-linux-lnt/stage1.install/bin/clang+0x316ce72)
[...]

that appears to be directly related to this visitCastInst change.

Thanks for letting me know! I'll push a fix shortly.