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

Uniform printing of simplified vs non-simplified #805

Merged
merged 2 commits into from
Nov 24, 2024
Merged

Conversation

elazarg
Copy link
Collaborator

@elazarg elazarg commented Nov 24, 2024

Fix small issues in simplification

  • Do not print entry and exit, and always print line details.
  • Pass simplify flag to collect_basic_blocks, and collect trivial ones if false.
  • Move all printing code to asm_ostream.cpp
  • Fix cfg standalone printing code: unify with printing invariants

Summary by CodeRabbit

  • New Features

    • Introduced a method to collect basic blocks from a control-flow graph with optional simplification.
    • Enhanced printing of invariants with a new optional simplification parameter.
  • Bug Fixes

    • Improved clarity and efficiency in the output of invariants by consolidating logic and removing redundancy.
  • Refactor

    • Streamlined access to verbosity options in the main program logic for better readability and maintainability.

Do not print entry and exit.

Signed-off-by: Elazar Gershuni <[email protected]>
Copy link

coderabbitai bot commented Nov 24, 2024

Walkthrough

The pull request introduces a new method collect_basic_blocks in the basic_block_t class, enhancing the functionality for gathering basic blocks from a control-flow graph (CFG). This method operates in two modes, controlled by a boolean parameter simplify. Additionally, it updates the method signature in cfg.hpp and modifies the print_invariants method in both crab_verifier.cpp and crab_verifier.hpp to incorporate the new boolean parameter. The changes also include refactoring verbosity handling in check.cpp, improving code readability without altering core functionality.

Changes

File Change Summary
src/asm_cfg.cpp Added method std::set<basic_block_t> basic_block_t::collect_basic_blocks(const cfg_t&, bool) to collect basic blocks with a simplify option.
src/crab/cfg.hpp Updated method signature from static std::set<basic_block_t> collect_basic_blocks(const cfg_t&) to static std::set<basic_block_t> collect_basic_blocks(const cfg_t&, bool).
src/crab_verifier.cpp Modified print_invariants method signature to include bool simplify and refactored logic to utilize it for collecting basic blocks. Removed LineInfoPrinter and several reporting methods from the Report class.
src/crab_verifier.hpp Updated method signature from void print_invariants(std::ostream&, const cfg_t&) to friend void print_invariants(std::ostream&, const cfg_t&, bool, const Invariants&).
src/main/check.cpp Refactored verbosity handling to use a local variable for improved readability, without altering core functionality.
src/asm_ostream.cpp Added LineInfoPrinter struct and several new functions for printing reports. Modified print_cfg to integrate line information handling.

Possibly related PRs


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 4

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

📥 Commits

Reviewing files that changed from the base of the PR and between 22625ba and e9d9933.

📒 Files selected for processing (5)
  • src/asm_cfg.cpp (1 hunks)
  • src/crab/cfg.hpp (1 hunks)
  • src/crab_verifier.cpp (1 hunks)
  • src/crab_verifier.hpp (1 hunks)
  • src/main/check.cpp (1 hunks)
🔇 Additional comments (8)
src/crab_verifier.cpp (2)

53-66: Implementation aligns with PR objectives

The code achieves uniform printing by:

  • Using LineInfoPrinter consistently for line information
  • Collecting basic blocks with the new simplify parameter
  • Following a clear printing sequence

52-52: Verify callers of print_invariants

The method signature has been updated to include the simplify parameter.

✅ Verification successful

All callers of print_invariants have been updated correctly

The method is called only once in src/main/check.cpp and the call site correctly passes the simplify parameter from verbosity.simplify. The method declaration in the header file also matches the implementation.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Find all callers of print_invariants to ensure they've been updated
# with the new parameter

rg "print_invariants\s*\([^)]*\)" --type cpp

Length of output: 373

src/main/check.cpp (3)

251-252: LGTM: Good refactoring

Extracting verbosity options into a local variable reduces redundancy.


253-253: Verify print_invariants implementation

Ensure the simplify parameter is properly handled in the print_invariants method.

✅ Verification successful

Simplify parameter is correctly implemented in print_invariants

The simplify parameter is properly handled in the print_invariants implementation in src/crab_verifier.cpp. It's passed directly to collect_basic_blocks() which uses it to control the simplification of basic blocks during printing.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for print_invariants implementation and its usage of the simplify parameter
ast-grep --pattern 'print_invariants($_,$_,$_) {
  $$$
}'

Length of output: 59


Script:

#!/bin/bash
# Search for print_invariants method with ripgrep for better coverage
rg -U "print_invariants.*\{[\s\S]*?\}" -A 10

# Also search for any print_invariants declarations or calls
rg "print_invariants" -A 2

Length of output: 4765


257-265: Check for duplicate termination messages

The termination message will be printed if either print_failures or print_invariants is true. This could result in duplicate output if both flags are enabled.

src/crab/cfg.hpp (1)

388-388: Document the simplify parameter and verify all call sites.

The signature change looks good but needs:

  1. Documentation for the simplify parameter to explain its behavior
  2. Updates to all call sites due to the breaking change

Run the following script to verify all call sites:

✅ Verification successful

All call sites are updated with the new parameter

The codebase search shows:

  • Implementation in src/asm_cfg.cpp correctly uses the new simplify parameter
  • Single call site in src/crab_verifier.cpp already passes the simplify parameter
  • No other call sites found
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Find all calls to collect_basic_blocks to ensure they're updated with the new parameter

# Search for method calls
rg -A 2 "collect_basic_blocks\(" 

# Search for method references
ast-grep --pattern 'collect_basic_blocks'

Length of output: 1038

src/asm_cfg.cpp (2)

253-262: LGTM: Non-simplify path correctly implements PR objectives

The implementation correctly excludes entry and exit points from collection, aligning with PR requirements.


288-292: Verify behavior when entry instruction is undefined

When the first label is the entry label, we clear m_ts but continue processing. This might lead to unexpected behavior.

✅ Verification successful

Clearing m_ts for entry label is correct behavior

The code is handling a special case correctly. For the entry label, m_ts is cleared before pushing the next label because:

  1. The entry instruction is undefined by design
  2. m_ts represents a sequence of labels, and starting with a clean slate (0) for entry block is the intended behavior
  3. The subsequent push_back ensures proper initialization with the next label
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for tests or code paths that exercise the entry instruction undefined case
rg -l "entry.*undefined" --type cpp
ast-grep --pattern 'if ($x == cfg.entry_label()) { $$$}'

Length of output: 96


Script:

#!/bin/bash
# Search for tests related to entry label and m_ts
rg -A 5 "entry_label" --type cpp

# Search for m_ts usage in BasicBlock class
ast-grep --pattern 'class BasicBlock {
  $$$
  m_ts $$$
  $$$
}'

# Search for test cases involving BasicBlock and m_ts
rg -A 5 "BasicBlock.*m_ts" --type cpp

Length of output: 4993


Script:

#!/bin/bash
# Look for BasicBlock class definition and m_ts member
rg -A 10 "class BasicBlock" --type cpp --type hpp

# Look for m_ts initialization or usage
rg -A 3 "m_ts\." --type cpp --type hpp
rg -A 3 "m_ts\[" --type cpp --type hpp

# Look for tests involving entry label and basic blocks
rg -A 5 "TEST.*BasicBlock.*entry" --type cpp

Length of output: 264


Script:

#!/bin/bash
# Look for BasicBlock class definition and m_ts member
rg -A 10 "class BasicBlock"

# Look for m_ts initialization or usage
rg -A 3 "m_ts\."
rg -A 3 "m_ts\["

# Look for any tests or code that handles the entry block case
rg -B 3 -A 3 "first_label.*entry"

Length of output: 1331

src/crab_verifier.hpp Outdated Show resolved Hide resolved
src/asm_cfg.cpp Show resolved Hide resolved
src/asm_cfg.cpp Show resolved Hide resolved
src/asm_cfg.cpp Outdated Show resolved Hide resolved
@coveralls
Copy link

coveralls commented Nov 24, 2024

Coverage Status

coverage: 89.665% (-0.1%) from 89.808%
when pulling d82d43f on simplify-print
into 22625ba on main.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 5

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

📥 Commits

Reviewing files that changed from the base of the PR and between e9d9933 and d82d43f.

📒 Files selected for processing (6)
  • src/asm_cfg.cpp (1 hunks)
  • src/asm_ostream.cpp (3 hunks)
  • src/crab/cfg.hpp (2 hunks)
  • src/crab_verifier.cpp (0 hunks)
  • src/crab_verifier.hpp (2 hunks)
  • src/main/check.cpp (1 hunks)
💤 Files with no reviewable changes (1)
  • src/crab_verifier.cpp
🧰 Additional context used
📓 Learnings (1)
src/asm_cfg.cpp (1)
Learnt from: elazarg
PR: vbpf/ebpf-verifier#805
File: src/asm_cfg.cpp:264-267
Timestamp: 2024-11-24T12:00:20.806Z
Learning: In `collect_basic_blocks`, removing `next_label` requires `std::set` for `worklist`, so replacing it with `std::queue` is not appropriate.
🔇 Additional comments (6)
src/crab_verifier.hpp (1)

70-70: Verify friend function implementation.

Ensure the corresponding implementation in the cpp file properly handles the simplify parameter and maintains const correctness.

✅ Verification successful

Implementation matches declaration and maintains const correctness

The implementation in src/asm_ostream.cpp correctly:

  • Preserves const correctness for both cfg and invariants parameters
  • Matches the parameter order and types from the declaration
  • Properly handles the simplify parameter by forwarding it to print_cfg
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for the implementation to verify const correctness and parameter handling
rg -A 5 "void print_invariants.*simplify.*Invariants&"

Length of output: 891

src/main/check.cpp (2)

241-244: LGTM: Good refactoring to reduce structure access.

The introduction of the local verbosity variable improves readability by eliminating repeated access to the nested structure.


251-252: LGTM: Consistent with the uniform printing objective.

The print_invariants call correctly uses the simplify flag to maintain uniform printing behavior.

src/crab/cfg.hpp (2)

388-388: Verify implementation of collect_basic_blocks

The method signature change aligns with PR objectives for controlled simplification.

✅ Verification successful

Implementation matches declaration and callers are updated

The collect_basic_blocks implementation in src/asm_cfg.cpp correctly matches the declaration with both cfg and simplify parameters. The only caller in src/asm_ostream.cpp passes both required arguments.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify implementation matches declaration
# and all callers are updated

# Find implementation
ast-grep --pattern 'std::set<basic_block_t> collect_basic_blocks(const cfg_t& $_, bool $_) {
  $$$
}'

# Find callers
rg -l "collect_basic_blocks" --type cpp

Length of output: 196


Script:

#!/bin/bash
# Find implementation and callers with context
rg "collect_basic_blocks" -B2 -A2 --type cpp

# Find any potential operator<< that might need updating
rg "operator<<.*cfg_t" --type cpp

Length of output: 1149


422-422: Verify callers of removed operator<<

The new print_cfg function replaces the previous stream operator.

✅ Verification successful

Removal of operator<< is safe

Based on the codebase analysis:

  • No existing usages of operator<< with cfg_t found
  • The new print_cfg function is properly defined and implemented in src/asm_ostream.cpp
  • No stream operations using cfg objects were found in files that include cfg.hpp
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Find and verify all callers are updated

# Find potential callers still using operator<<
rg "operator<<.*cfg_t"

# Find new print_cfg usage
rg "print_cfg.*cfg_t"

Length of output: 405


Script:

#!/bin/bash
# Search for any usage of operator<< with cfg_t in the git history
git log -p | rg "operator<<.*cfg_t"

# Find all usages of cfg_t with stream operations
rg "<<.*cfg" -A 2

Length of output: 19


Script:

#!/bin/bash
# Find all files that include cfg.hpp
rg -l "cfg\.hpp"

# Find any stream operations with cfg in these files
rg "os\s*<<.*cfg|cout\s*<<.*cfg|cerr\s*<<.*cfg" -A 2

Length of output: 350

src/asm_ostream.cpp (1)

208-209: Ensure Consistent Use of Simplify Parameter

In print_invariants, the simplify parameter is passed to print_cfg. Verify that this aligns with the intended behavior and that all calls to print_invariants provide the correct simplify value.

src/crab_verifier.hpp Show resolved Hide resolved
src/main/check.cpp Show resolved Hide resolved
src/asm_cfg.cpp Show resolved Hide resolved
src/asm_ostream.cpp Show resolved Hide resolved
src/asm_ostream.cpp Show resolved Hide resolved
@elazarg elazarg merged commit e09c6a7 into main Nov 24, 2024
19 checks passed
@elazarg elazarg deleted the simplify-print branch November 24, 2024 14:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants