Skip to content

Commit

Permalink
optimizing separately with wasm-opt (from binaryen 108) (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
vbar authored Jun 13, 2022
1 parent 1d79bc1 commit 7ed1c44
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
58 changes: 47 additions & 11 deletions c2wasm-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fastify from 'fastify';
import fs from 'fs';
import { mkdirSync, writeFileSync, existsSync, openSync, closeSync, readFileSync, readdirSync, rmSync, unlinkSync, fstat } from "fs";
import { mkdirSync, writeFileSync, existsSync, openSync, closeSync, readFileSync, renameSync, readdirSync, rmSync, unlinkSync, fstat } from "fs";
import { deflateSync } from "zlib";
import { execSync } from "child_process";
import { z } from 'zod';
Expand Down Expand Up @@ -100,13 +100,9 @@ function shell_exec(cmd: string, cwd: string) {
return result;
}

function get_clang_options(options: string) {
const clang_flags = `--sysroot=${sysroot} -xc -I/app/clang/includes -fdiagnostics-print-source-range-info -Werror=implicit-function-declaration`;
function get_optimization_options(options: string) {
const optimization_options = [
'-O0', '-O1', '-O2', '-O3', '-O4', '-Os'
];
const miscellaneous_options = [
'-ffast-math', '-fno-inline', '-std=c99', '-std=c89'
/* default '-O0' not included */ '-O1', '-O2', '-O3', '-O4', '-Os'
];

let safe_options = '';
Expand All @@ -116,10 +112,16 @@ function get_clang_options(options: string) {
}
}

if (!safe_options) {
safe_options += ' -O0';
}
return safe_options;
}

function get_clang_options(options: string) {
const clang_flags = `--sysroot=${sysroot} -xc -I/app/clang/includes -fdiagnostics-print-source-range-info -Werror=implicit-function-declaration`;
const miscellaneous_options = [
'-ffast-math', '-fno-inline', '-std=c99', '-std=c89'
];

let safe_options = '';
for (let o of miscellaneous_options) {
if (options.includes(o)) {
safe_options += ' ' + o;
Expand Down Expand Up @@ -182,6 +184,29 @@ function link_c_files(source_files: string[], compile_options: string, link_opti
return true;
}

function optimize_wasm(cwd: string, inplace: string, opt_options: string, result_obj: Task) {
const unopt = cwd + '/unopt.wasm';
const cmd = 'wasm-opt ' + opt_options + ' -o ' + inplace + ' ' + unopt;
const out = openSync(cwd + '/opt.log', 'w');
let error = '';
let success = true;
try {
renameSync(inplace, unopt);
execSync(cmd, { cwd, stdio: [null, out, out], });
} catch (ex: unknown) {
success = false;
if (ex instanceof Error) {
error = ex?.message;
}
} finally {
closeSync(out);
}
const out_msg = readFileSync(cwd + '/opt.log').toString() || error;
result_obj.console = sanitize_shell_output(out_msg);
result_obj.success = success;
return success;
}

function clean_wasm(cwd: string, inplace: string, result_obj: Task) {
const cmd = 'hook-cleaner ' + inplace;
const out = openSync(cwd + '/cleanout.log', 'w');
Expand All @@ -199,7 +224,7 @@ function clean_wasm(cwd: string, inplace: string, result_obj: Task) {
}
const out_msg = readFileSync(cwd + '/cleanout.log').toString() || error;
result_obj.console = sanitize_shell_output(out_msg);
result_obj.success = false;
result_obj.success = success;
return success;
}

Expand Down Expand Up @@ -274,6 +299,17 @@ function build_project(project: RequestBody, base: string) {
return complete(false, 'Build error');
}

const opt_options = get_optimization_options(options || '');
if (opt_options) {
const opt_obj = {
name: 'optimizing wasm'
};
build_result.tasks.push(opt_obj);
if (!optimize_wasm(dir, result, opt_options, opt_obj)) {
return complete(false, 'Optimization error');
}
}

if (strip) {
const clean_obj = {
name: 'cleaning wasm'
Expand Down
1 change: 1 addition & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ COPY c2wasm-api/tsconfig.json .
COPY c2wasm-api/src ./src
COPY wasi-sdk ./clang/wasi-sdk
COPY hook-cleaner /usr/bin
COPY wasm-opt /usr/bin
COPY run.sh .
ADD compile_flags.txt /etc/clangd/compile_flags.txt
ADD .clang-tidy /work/.clang-tidy
Expand Down
Binary file added docker/wasm-opt
Binary file not shown.

0 comments on commit 7ed1c44

Please sign in to comment.