## Environment
We recommend python 3.10 (tested) considering the dependency requirements.
```
pip install requirements.txt
```

## Raplacing Code Path
For experimental purposes, we recommend using full paths to adapt most benchmarks.

#### Loading Benchs
`*.json` in `loadbenchs` should follow this structure:
```json
[
    {
        "path": "<project_path>/benchmarks/cbench_demo/automotive_qsort1",
        "ldflags": " -no-pie -DFPM_DEFAULT -lmp3lame -lm -lstdc++ -lz -lpng  -lfreetype ",
        "runflags": " needoutput <project_path>/benchmarks/cbench_demo/automotive_qsort_data/1.dat output.dat "
    }
]
```
- `"path"`: The path of benchmark code.
- `"ldflags"`: The flags of linking stage.
- `"runflags"`: Flags to run binary, needed by `perf_.sh`.

Replace all `<project_path>` to real path.


#### LLVM Compiler
Replace all `<llvm_path>` to real LLVM toolchain path.

Files:
- `benchmarks/cbench_demo/all_compile`
- `scripts/extract_features.sh`
- `utils/tools.py`

#### Python Interpreter
Replace all `<python_interpreter_path>` to real path of `python` executable binary.

Files:
- `scripts/remote_exe/remote_exe.py`
- `scripts/input_json_gen.py`
- `scripts/merge_remarks_no_order.py`
- `benchmarks/cbench_demo/input_schema_gen.py`

## Platform Info
`lscpu` get the cpu info of target machine. Fill the `PLATFORM_INFO` in `AutoPassWorker.py`. 

For example:
```python
PLATFORM_INFO = PlatformInfo(
    target_arch="x86_64",
    compiler="clang 1x.x.x",
    cpu="<cpu name>",
    freq="3.5 GHz",
    L1dcache="256KB",
    L1icache="256KB",
    L2cache="4MB",
    L3cache="8MB",
)
```

## LLM Agents
This project employs CrewAI to construct a multi-agent system.

Some LLM models cannot meet the context requirements of the Potential Score Agent (such as ChatGPT 4o). The system permits the use of alternative models to initialize this agent, as the core agents are Reasoning and Evaluation.

In file `utils/agents.py`:
- replace `<API_KEY>` to real api key.
- replace `<BASE_URL>` to the model's url.

These settings can be written to a `.env` file, and this is the recommended approach.

## Run AutoPass
First, you need to convert the code into unoptimized LLVM IR (see `benchmarks/cbench_demo/all_compile` for reference).

Then:
```bash
cd sources  # from project root
chmod +x -R ./scripts
python ./RunAutoPass.py
```

#### Setting More Workers to Parallel LLM Generation
Change the value of `MAX_WORKERS` in file `RunAutoPass.py`.


#### Remote AutoPass
To run AutoPass on a remote machine, you need a LLVM toolchain that supports cross-compilation. This allows AutoPass to perform optimization on a low-end host (such as Raspberry Pi) without agent-system overhead.

Fill in the remote machine configuration in `scripts/remote_exe/remote_configs.json`; the following fields are required:

- machine alias
- remote host
- remote user
- SSH private key path (must be generated and stored properly for passwordless login)
- test directory on the remote machine
- local path to the remote `perf script` (you may need to adapt this script, as `perf` events can vary by system)
- cross-compile target (AutoPass can infer target from `platform_info`, but baseline compilation target should be set explicitly)
- platform info JSON


## Data processing
Run `data_process.py` to get basic speedup ratios.