Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[InstrProf] Change step from 64-bit to pointer-sized int #83239

Closed
wants to merge 1 commit into from

Conversation

jdmitrovic-syrmia
Copy link

Fixed 64-bit step can lead to creating atomic instructions unsupported by the target architecture (see rust-lang/rust#112313).

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Collaborator

llvmbot commented Feb 28, 2024

@llvm/pr-subscribers-pgo
@llvm/pr-subscribers-llvm-transforms

@llvm/pr-subscribers-llvm-ir

Author: Jovan Dmitrović (jdmitrovic-syrmia)

Changes

Fixed 64-bit step can lead to creating atomic instructions unsupported by the target architecture (see rust-lang/rust#112313).


Full diff: https://github.com/llvm/llvm-project/pull/83239.diff

1 Files Affected:

  • (modified) llvm/lib/IR/IntrinsicInst.cpp (+2-1)
diff --git a/llvm/lib/IR/IntrinsicInst.cpp b/llvm/lib/IR/IntrinsicInst.cpp
index 89403e1d7fcb4d..0f0a6bf23ffe43 100644
--- a/llvm/lib/IR/IntrinsicInst.cpp
+++ b/llvm/lib/IR/IntrinsicInst.cpp
@@ -288,7 +288,8 @@ Value *InstrProfIncrementInst::getStep() const {
   }
   const Module *M = getModule();
   LLVMContext &Context = M->getContext();
-  return ConstantInt::get(Type::getInt64Ty(Context), 1);
+  const auto &DL = M->getDataLayout();
+  return ConstantInt::get(DL.getIntPtrType(Context), 1);
 }
 
 std::optional<RoundingMode> ConstrainedFPIntrinsic::getRoundingMode() const {

@@ -288,7 +288,8 @@ Value *InstrProfIncrementInst::getStep() const {
}
const Module *M = getModule();
LLVMContext &Context = M->getContext();
return ConstantInt::get(Type::getInt64Ty(Context), 1);
const auto &DL = M->getDataLayout();
return ConstantInt::get(DL.getIntPtrType(Context), 1);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With a 64-bit type, very few counters will overflow. With a pointer-width type, more counters will overflow, especially on 16-bit targets, but even on 32-bit targets. I speculate this is why 64-bit integers are used currently. How should we address this concern?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I pushed some changes, creating additional getStep function for atomics. This way, we can use a larger type.

Also, I've taken the liberty of changing the existing code to use the largest legal int available instead of the 64-bit int.

Copy link

github-actions bot commented Mar 1, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@briansmith
Copy link

@jdmitrovic-syrmia I would love to review this but I am not an LLVM project member and I don't understand the implementation well enough to offer useful feedback on the implementation.

For my interests, I am primarily interested in code coverage and not profiling.

I also found https://discourse.llvm.org/t/rfc-single-byte-counters-for-source-based-code-coverage/75685 which addresses the problem in a different way. Note that it is claimed there that "A nice advantage of single-byte counters is that it is already thread-safe without needing atomics." It was just merged a couple of weeks ago; see #75425.

Fixed 64-bit step can lead to creating atomic instructions
unsupported by the target architecture (see rust-lang/rust#112313).

When using atomics, type of the step is a pointer-sized integer.
Otherwise, type of the step is of a largest legal integer type.
@jdmitrovic-syrmia
Copy link
Author

@briansmith It seems to me that #75425 concerns code coverage, and not profiling. I don't think this PR is affecting the code coverage analysis. Also, #75425 doesn't resolve the Rust issue I mentioned.

@MaskRay
Copy link
Member

MaskRay commented Mar 19, 2024

This is incorrect. See rust-lang/rust#112313
In the absence of __sync_fetch_and_add_8, the best is to consider the feature unusable on Rust's mips port.

@MaskRay MaskRay closed this Mar 19, 2024
@briansmith
Copy link

@jdmitrovic-syrmia As an alternative solution, why not implement a (perhaps slow) atomic 64-bit fallback implementation that uses a global lock?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:ir llvm:transforms PGO Profile Guided Optimizations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants