Skip to content

Commit

Permalink
Don't try and hack the compilation results (#4429)
Browse files Browse the repository at this point in the history
* Don't try and hack the compilation results

Carbon tried to synthesize "having run" by overriding and hacking a result routine...but did so _after_ it had been cached. So cached results were broken, but live results not.

This "fixes" by adding a post-compilation, but pre-cache hook and uses that instead. Naming is terrible.

I also took the time to fix the `CompilationResult`'s `buildResult`

Closes #4426
  • Loading branch information
mattgodbolt committed Dec 13, 2022
1 parent 9de0b70 commit cad5227
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 27 deletions.
2 changes: 1 addition & 1 deletion etc/config/carbon.defaults.properties
Expand Up @@ -6,7 +6,7 @@ needsMulti=false
supportsExecute=false
supportsBinary=false

compiler.carbon-trunkdef.exe=todo-default-path
compiler.carbon-trunkdef.exe=/opt/compiler-explorer/carbon-trunk/bin/carbon-explorer
compiler.carbon-trunkdef.name=Explorer (trunk)
compiler.carbon-trunkdef.alias=carbon-trunk

Expand Down
6 changes: 6 additions & 0 deletions lib/base-compiler.ts
Expand Up @@ -2324,6 +2324,8 @@ export class BaseCompiler implements ICompiler {

if (!backendOptions.skipPopArgs) result.popularArguments = this.possibleArguments.getPopularArguments(options);

result = this.postCompilationPreCacheHook(result);

if (result.okToCache) {
await this.env.cachePut(key, result);
}
Expand All @@ -2338,6 +2340,10 @@ export class BaseCompiler implements ICompiler {
return result;
}

postCompilationPreCacheHook(result: CompilationResult): CompilationResult {
return result;
}

processAsm(result, filters, options) {
if ((options && options.includes('-emit-llvm')) || this.llvmIr.isLlvmIr(result.asm)) {
return this.llvmIr.process(result.asm, filters);
Expand Down
44 changes: 18 additions & 26 deletions lib/compilers/carbon.ts
Expand Up @@ -23,8 +23,10 @@
// POSSIBILITY OF SUCH DAMAGE.

import {ParsedAsmResult} from '../../types/asmresult/asmresult.interfaces';
import {CompilationResult} from '../../types/compilation/compilation.interfaces';
import {CompilerInfo} from '../../types/compiler.interfaces';
import {ParseFiltersAndOutputOptions} from '../../types/features/filters.interfaces';
import {ResultLine} from '../../types/resultline/resultline.interfaces';
import {BaseCompiler} from '../base-compiler';

import {BaseParser} from './argument-parsers';
Expand Down Expand Up @@ -65,41 +67,31 @@ export class CarbonCompiler extends BaseCompiler {
);
}

override async afterCompilation(
result,
doExecute,
key,
executeParameters,
tools,
backendOptions,
filters,
options,
optOutput,
customBuildPath,
) {
result = await super.afterCompilation(
result,
doExecute,
key,
executeParameters,
tools,
backendOptions,
filters,
options,
optOutput,
customBuildPath,
);
lastLine(lines?: ResultLine[]): string {
if (!lines || lines.length === 0) return '';
return (lines.at(-1) as ResultLine).text;
}

override postCompilationPreCacheHook(result: CompilationResult): CompilationResult {
if (result.code === 0) {
// Hook to parse out the "result: 123" line at the end of the interpreted execution run.
const re = /^result: (\d+)$/;
const match = re.exec(result.asm.at(-1).text);
const match = re.exec(this.lastLine(result.asm));
const code = match ? parseInt(match[1]) : -1;
result.execResult = {
stdout: result.stdout,
stderr: [],
code: code,
didExecute: true,
buildResult: {code: 0},
buildResult: {
code: 0,
timedOut: false,
stdout: [],
stderr: [],
downloads: [],
executableFilename: '',
compilationOptions: [],
},
};
result.stdout = [];
}
Expand Down
3 changes: 3 additions & 0 deletions types/compilation/compilation.interfaces.ts
Expand Up @@ -130,6 +130,9 @@ export type BuildResult = CompilationResult & {
downloads: BuildEnvDownloadInfo[];
executableFilename: string;
compilationOptions: string[];
stdout: ResultLine[];
stderr: ResultLine[];
code: number;
};

export type BuildStep = BasicExecutionResult & {
Expand Down

0 comments on commit cad5227

Please sign in to comment.