Skip to content

Commit 7ed1c44

Browse files
authored
optimizing separately with wasm-opt (from binaryen 108) (#18)
1 parent 1d79bc1 commit 7ed1c44

File tree

3 files changed

+48
-11
lines changed

3 files changed

+48
-11
lines changed

c2wasm-api/src/index.ts

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fastify from 'fastify';
22
import fs from 'fs';
3-
import { mkdirSync, writeFileSync, existsSync, openSync, closeSync, readFileSync, readdirSync, rmSync, unlinkSync, fstat } from "fs";
3+
import { mkdirSync, writeFileSync, existsSync, openSync, closeSync, readFileSync, renameSync, readdirSync, rmSync, unlinkSync, fstat } from "fs";
44
import { deflateSync } from "zlib";
55
import { execSync } from "child_process";
66
import { z } from 'zod';
@@ -100,13 +100,9 @@ function shell_exec(cmd: string, cwd: string) {
100100
return result;
101101
}
102102

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

112108
let safe_options = '';
@@ -116,10 +112,16 @@ function get_clang_options(options: string) {
116112
}
117113
}
118114

119-
if (!safe_options) {
120-
safe_options += ' -O0';
121-
}
115+
return safe_options;
116+
}
122117

118+
function get_clang_options(options: string) {
119+
const clang_flags = `--sysroot=${sysroot} -xc -I/app/clang/includes -fdiagnostics-print-source-range-info -Werror=implicit-function-declaration`;
120+
const miscellaneous_options = [
121+
'-ffast-math', '-fno-inline', '-std=c99', '-std=c89'
122+
];
123+
124+
let safe_options = '';
123125
for (let o of miscellaneous_options) {
124126
if (options.includes(o)) {
125127
safe_options += ' ' + o;
@@ -182,6 +184,29 @@ function link_c_files(source_files: string[], compile_options: string, link_opti
182184
return true;
183185
}
184186

187+
function optimize_wasm(cwd: string, inplace: string, opt_options: string, result_obj: Task) {
188+
const unopt = cwd + '/unopt.wasm';
189+
const cmd = 'wasm-opt ' + opt_options + ' -o ' + inplace + ' ' + unopt;
190+
const out = openSync(cwd + '/opt.log', 'w');
191+
let error = '';
192+
let success = true;
193+
try {
194+
renameSync(inplace, unopt);
195+
execSync(cmd, { cwd, stdio: [null, out, out], });
196+
} catch (ex: unknown) {
197+
success = false;
198+
if (ex instanceof Error) {
199+
error = ex?.message;
200+
}
201+
} finally {
202+
closeSync(out);
203+
}
204+
const out_msg = readFileSync(cwd + '/opt.log').toString() || error;
205+
result_obj.console = sanitize_shell_output(out_msg);
206+
result_obj.success = success;
207+
return success;
208+
}
209+
185210
function clean_wasm(cwd: string, inplace: string, result_obj: Task) {
186211
const cmd = 'hook-cleaner ' + inplace;
187212
const out = openSync(cwd + '/cleanout.log', 'w');
@@ -199,7 +224,7 @@ function clean_wasm(cwd: string, inplace: string, result_obj: Task) {
199224
}
200225
const out_msg = readFileSync(cwd + '/cleanout.log').toString() || error;
201226
result_obj.console = sanitize_shell_output(out_msg);
202-
result_obj.success = false;
227+
result_obj.success = success;
203228
return success;
204229
}
205230

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

302+
const opt_options = get_optimization_options(options || '');
303+
if (opt_options) {
304+
const opt_obj = {
305+
name: 'optimizing wasm'
306+
};
307+
build_result.tasks.push(opt_obj);
308+
if (!optimize_wasm(dir, result, opt_options, opt_obj)) {
309+
return complete(false, 'Optimization error');
310+
}
311+
}
312+
277313
if (strip) {
278314
const clean_obj = {
279315
name: 'cleaning wasm'

docker/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ COPY c2wasm-api/tsconfig.json .
1111
COPY c2wasm-api/src ./src
1212
COPY wasi-sdk ./clang/wasi-sdk
1313
COPY hook-cleaner /usr/bin
14+
COPY wasm-opt /usr/bin
1415
COPY run.sh .
1516
ADD compile_flags.txt /etc/clangd/compile_flags.txt
1617
ADD .clang-tidy /work/.clang-tidy

docker/wasm-opt

8.6 MB
Binary file not shown.

0 commit comments

Comments
 (0)