Skip to content

Commit

Permalink
Add support for -Zunpretty=hir
Browse files Browse the repository at this point in the history
  • Loading branch information
jyn514 committed Feb 21, 2021
1 parent a19bffb commit df4b0c6
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 3 deletions.
15 changes: 14 additions & 1 deletion ui/frontend/BuildMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ const useDispatchAndClose = (action: () => void, close: () => void) => {
}

const BuildMenu: React.SFC<BuildMenuProps> = props => {
const isHirAvailable = useSelector(selectors.isHirAvailable);
const isWasmAvailable = useSelector(selectors.isWasmAvailable);

const compile = useDispatchAndClose(actions.performCompile, props.close);
const compileToAssembly = useDispatchAndClose(actions.performCompileToAssembly, props.close);
const compileToLLVM = useDispatchAndClose(actions.performCompileToLLVM, props.close);
const compileToMir = useDispatchAndClose(actions.performCompileToMir, props.close);
const compileToHir = useDispatchAndClose(actions.performCompileToNightlyHir, props.close);
const compileToWasm = useDispatchAndClose(actions.performCompileToNightlyWasm, props.close);
const execute = useDispatchAndClose(actions.performExecute, props.close);
const test = useDispatchAndClose(actions.performTest, props.close);
Expand All @@ -55,7 +57,11 @@ const BuildMenu: React.SFC<BuildMenuProps> = props => {
Build and show the resulting LLVM IR, LLVM’s intermediate representation.
</ButtonMenuItem>
<ButtonMenuItem name="MIR" onClick={compileToMir}>
Build and show the resulting MIR, Rust’s intermediate representation.
Build and show the resulting MIR, Rust’s intermediate representation for control flow.
</ButtonMenuItem>
<ButtonMenuItem name="HIR" onClick={compileToHir}>
Build and show the resulting HIR, Rust’s intermediate representation for items in the current crate.
{!isHirAvailable && <HirAside />}
</ButtonMenuItem>
<ButtonMenuItem name="WASM" onClick={compileToWasm}>
Build a WebAssembly module for web browsers, in the .WAT textual representation.
Expand All @@ -65,6 +71,13 @@ const BuildMenu: React.SFC<BuildMenuProps> = props => {
);
};

const HirAside: React.SFC = () => (
<p className="build-menu__aside">
Note: HIR currently requires using the Nightly channel, selecting this
option will switch to Nightly.
</p>
);

const WasmAside: React.SFC = () => (
<p className="build-menu__aside">
Note: WASM currently requires using the Nightly channel, selecting this
Expand Down
27 changes: 27 additions & 0 deletions ui/frontend/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ export enum ActionType {
CompileLlvmIrRequest = 'COMPILE_LLVM_IR_REQUEST',
CompileLlvmIrSucceeded = 'COMPILE_LLVM_IR_SUCCEEDED',
CompileLlvmIrFailed = 'COMPILE_LLVM_IR_FAILED',
CompileHirRequest = 'COMPILE_HIR_REQUEST',
CompileHirSucceeded = 'COMPILE_HIR_SUCCEEDED',
CompileHirFailed = 'COMPILE_HIR_FAILED',
CompileMirRequest = 'COMPILE_MIR_REQUEST',
CompileMirSucceeded = 'COMPILE_MIR_SUCCEEDED',
CompileMirFailed = 'COMPILE_MIR_FAILED',
Expand Down Expand Up @@ -346,6 +349,27 @@ const performCompileToLLVMOnly = () =>
failure: receiveCompileLlvmIrFailure,
});

const requestCompileHir = () =>
createAction(ActionType.CompileHirRequest);

const receiveCompileHirSuccess = ({ code, stdout, stderr }) =>
createAction(ActionType.CompileHirSucceeded, { code, stdout, stderr });

const receiveCompileHirFailure = ({ error }) =>
createAction(ActionType.CompileHirFailed, { error });

const performCompileToHirOnly = () =>
performCompileShow('hir', {
request: requestCompileHir,
success: receiveCompileHirSuccess,
failure: receiveCompileHirFailure,
});

const performCompileToNightlyHirOnly = (): ThunkAction => dispatch => {
dispatch(changeChannel(Channel.Nightly));
dispatch(performCompileToHirOnly());
};

const requestCompileMir = () =>
createAction(ActionType.CompileMirRequest);

Expand Down Expand Up @@ -390,6 +414,7 @@ const PRIMARY_ACTIONS: { [index in PrimaryAction]: () => ThunkAction } = {
[PrimaryActionCore.Test]: performTestOnly,
[PrimaryActionAuto.Auto]: performAutoOnly,
[PrimaryActionCore.LlvmIr]: performCompileToLLVMOnly,
[PrimaryActionCore.Hir]: performCompileToHirOnly,
[PrimaryActionCore.Mir]: performCompileToMirOnly,
[PrimaryActionCore.Wasm]: performCompileToNightlyWasmOnly,
};
Expand Down Expand Up @@ -417,6 +442,8 @@ export const performCompileToLLVM =
performAndSwitchPrimaryAction(performCompileToLLVMOnly, PrimaryActionCore.LlvmIr);
export const performCompileToMir =
performAndSwitchPrimaryAction(performCompileToMirOnly, PrimaryActionCore.Mir);
export const performCompileToNightlyHir =
performAndSwitchPrimaryAction(performCompileToNightlyHirOnly, PrimaryActionCore.Hir);
export const performCompileToNightlyWasm =
performAndSwitchPrimaryAction(performCompileToNightlyWasmOnly, PrimaryActionCore.Wasm);

Expand Down
5 changes: 4 additions & 1 deletion ui/frontend/selectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const LABELS: { [index in PrimaryActionCore]: string } = {
[PrimaryActionCore.Compile]: 'Build',
[PrimaryActionCore.Execute]: 'Run',
[PrimaryActionCore.LlvmIr]: 'Show LLVM IR',
[PrimaryActionCore.Hir]: 'Show HIR',
[PrimaryActionCore.Mir]: 'Show MIR',
[PrimaryActionCore.Test]: 'Test',
[PrimaryActionCore.Wasm]: 'Show WASM',
Expand Down Expand Up @@ -102,9 +103,11 @@ export const miriVersionDetailsText = createSelector([getMiri], versionDetails);

const editionSelector = (state: State) => state.configuration.edition;

export const isWasmAvailable = (state: State) => (
export const isNightlyChannel = (state: State) => (
state.configuration.channel === Channel.Nightly
);
export const isWasmAvailable = isNightlyChannel;
export const isHirAvailable = isNightlyChannel;

export const getModeLabel = (state: State) => {
const { configuration: { mode } } = state;
Expand Down
1 change: 1 addition & 0 deletions ui/frontend/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export enum PrimaryActionCore {
Compile = 'compile',
Execute = 'execute',
LlvmIr = 'llvm-ir',
Hir = 'hir',
Mir = 'mir',
Test = 'test',
Wasm = 'wasm',
Expand Down
1 change: 1 addition & 0 deletions ui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,7 @@ fn parse_target(s: &str) -> Result<sandbox::CompileTarget> {
sandbox::ProcessAssembly::Filter),
"llvm-ir" => sandbox::CompileTarget::LlvmIr,
"mir" => sandbox::CompileTarget::Mir,
"hir" => sandbox::CompileTarget::Hir,
"wasm" => sandbox::CompileTarget::Wasm,
value => InvalidTarget { value }.fail()?,
})
Expand Down
14 changes: 13 additions & 1 deletion ui/src/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ impl Sandbox {
if process == ProcessAssembly::Filter {
code = super::asm_cleanup::filter_asm(&code);
}
} else if CompileTarget::Hir == req.target {
// TODO: Run rustfmt on the generated HIR.
}

Ok(CompileResponse {
Expand Down Expand Up @@ -479,7 +481,13 @@ fn build_execution_command(target: Option<CompileTarget>, channel: Channel, mode
}

if let Some(target) = target {
cmd.extend(&["--", "-o", "/playground-result/compilation"]);
cmd.extend(&["--", "-o"]);
if target == Hir {
// -Zunpretty=hir only emits the HIR, not the binary itself
cmd.push("/playground-result/compilation.hir");
} else {
cmd.push("/playground-result/compilation");
}

match target {
Assembly(flavor, _, _) => {
Expand All @@ -501,6 +509,7 @@ fn build_execution_command(target: Option<CompileTarget>, channel: Channel, mode
},
LlvmIr => cmd.push("--emit=llvm-ir"),
Mir => cmd.push("--emit=mir"),
Hir => cmd.push("-Zunpretty=hir"),
Wasm => { /* handled by cargo-wasm wrapper */ },
}
}
Expand Down Expand Up @@ -612,6 +621,7 @@ pub enum CompileTarget {
Assembly(AssemblyFlavor, DemangleAssembly, ProcessAssembly),
LlvmIr,
Mir,
Hir,
Wasm,
}

Expand All @@ -621,6 +631,7 @@ impl CompileTarget {
CompileTarget::Assembly(_, _, _) => "s",
CompileTarget::LlvmIr => "ll",
CompileTarget::Mir => "mir",
CompileTarget::Hir => "hir",
CompileTarget::Wasm => "wat",
};
OsStr::new(ext)
Expand All @@ -635,6 +646,7 @@ impl fmt::Display for CompileTarget {
Assembly(_, _, _) => "assembly".fmt(f),
LlvmIr => "LLVM IR".fmt(f),
Mir => "Rust MIR".fmt(f),
Hir => "Rust HIR".fmt(f),
Wasm => "WebAssembly".fmt(f),
}
}
Expand Down

0 comments on commit df4b0c6

Please sign in to comment.