project-mahiwa / mahiwa-frontend-go

mahiwaのGo言語向けフロントエンド
https://pkg.go.dev/github.com/project-mahiwa/mahiwa-frontend-go
MIT License
0 stars 0 forks source link

[🛠️Refactor]:ビルド時のスタックサイズなどのパラメータを見直す #29

Open usuyuki opened 10 months ago

usuyuki commented 10 months ago

🛠️ 概要

言語側,マイコン側がそれぞれ作用していて,適切な値を決めるのが難しい. (一応触るのはユーザー側なので,ユーザーにおまかせという形はあるが,どのくらいまで問題ないのか,どのくらいないと止まるのかの大雑把な把握だけでもして起きたい)

なぜ必要

備考

usuyuki commented 10 months ago

zstack-sizeとかのldflagsの単位すらわからない.

usuyuki commented 10 months ago
    "--strip-all"

つけてみたら

[wasm-validator error in function 51] unexpected false: Bulk memory operations require bulk memory [--enable-bulk
-memory], on 
(memory.copy
 (local.get $9)
 (local.get $2)
 (i32.load offset=16
  (local.get $0)
 )
)
Fatal: error validating input

と怒られた.

usuyuki commented 10 months ago

-ldflags The TinyGo ldflags flag has the same behavior as the Go link tool ldflags flag. It passes a value along to the TinyGo linker. This is commonly used to set the value for a variable at the time that the program is compiled for example -ldflags="-X 'package_path.variable_name=new_value'"

https://tinygo.org/docs/reference/usage/important-options/

そもそもgoのldflgsの情報が全然ない.コンパイラに与えられる変数みたいなことしか出てこない.

usuyuki commented 10 months ago
  "ldflags": ["-help"]

でbuildしてhelpを出してみる.

tinygo build -target ./target.json -o main.wasm main.go
OVERVIEW: LLVM Linker

USAGE: tinygo:wasm-ld [options] file...

OPTIONS:
  --allow-undefined-file=<value>
                          Allow symbols listed in <file> to be undefined in linked binary
  --allow-undefined       Allow undefined symbols in linked binary. This options is equivalent to --import-undefi
ned and --unresolved-symbols=ignore-all
  --Bdynamic              Link against shared libraries (default)
  --Bstatic               Do not link against shared libraries
  --Bsymbolic             Bind defined symbols locally
  --call_shared           Alias for --Bdynamic
  --check-features        Check feature compatibility of linked objects (default)
  --color-diagnostics=[auto,always,never]
                          Use colors in diagnostics (default: auto)
  --color-diagnostics     Alias for --color-diagnostics=always
  --compress-relocations  Compress the relocation targets in the code section.
  --demangle              Demangle symbol names
  --dn                    Alias for --Bstatic
  --dy                    Alias for --Bdynamic
  --emit-relocs           Generate relocations in output
  --entry <entry>         Name of entry point symbol
  --error-limit=<value>   Maximum number of errors to emit before stopping (0 = no limit)
  --error-unresolved-symbols
                          Report unresolved symbols as errors
  --experimental-pic      Enable Experimental PIC
  --export-all            Export all symbols (normally combined with --no-gc-sections)
  --export-dynamic        Put symbols in the dynamic symbol table
  --export-if-defined=<value>
                          Force a symbol to be exported, if it is defined in the input
  --export-memory=<value> Export the module's memory with the passed name
  --export-memory         Export the module's memory with the default name of "memory"
  --export-table          Export function table to the environment
  --export=<value>        Force a symbol to be exported
  --extra-features=<value>
                          Comma-separated list of features to add to the default set of features inferred from in
put objects.
  -E                      Alias for --export-dynamic
  --fatal-warnings        Treat warnings as errors
  --features=<value>      Comma-separated used features, inferred from input objects by default.
  --gc-sections           Enable garbage collection of unused sections
  --global-base=<value>   Where to start to place global data
  --growable-table        Remove maximum size from function table, allowing table to grow
  --help                  Print option help
  --import-memory=<module>,<name>
                          Import the module's memory from the passed module with the passed name.
  --import-memory         Import the module's memory from the default module of "env" with the name "memory".
  --import-table          Import function table from the environment
  --import-undefined      Turn undefined symbols into imports where possible
  --initial-memory=<value>
                          Initial size of the linear memory
  --lto-debug-pass-manager
                          Debug new pass manager
  --lto-O<opt-level>      Optimization level for LTO
  --lto-partitions=<value>
                          Number of LTO codegen partitions
  -L <dir>                Add a directory to the library search path
  -l <libName>            Root name of library to use
  --Map=<value>           Print a link map to the specified file
  --max-memory=<value>    Maximum size of the linear memory
  --merge-data-segments   Enable merging data segments
  --mllvm=<value>         Additional arguments to forward to LLVM's option processing
  -M                      Alias for --print-map
  -m <value>              Set target emulation
  --no-check-features     Ignore feature compatibility of linked objects
  --no-color-diagnostics  Alias for --color-diagnostics=never
  --no-demangle           Do not demangle symbol names
  --no-entry              Do not output any entry point
  --no-export-dynamic     Do not put symbols in the dynamic symbol table (default)
  --no-gc-sections        Disable garbage collection of unused sections
  --no-lto-legacy-pass-manager
                          Use new pass manager
  --no-merge-data-segments
                          Disable merging data segments
  --no-pie                Do not create a position independent executable (default)
  --no-print-gc-sections  Do not list removed unused sections
  --no-whole-archive      Do not force load of all members in a static library (default)
  --non_shared            Alias for --Bstatic
  -O <value>              Optimize output file size
  -o <path>               Path to file to write output
  --pie                   Create a position independent executable
  --print-gc-sections     List removed unused sections
  --print-map             Print a link map to the standard output
  --relocatable           Create relocatable object file
  --reproduce=<value>     Dump linker invocation and input files for debugging
  --rsp-quoting=[posix,windows]
                          Quoting style for response files
  --save-temps            Save intermediate LTO compilation results
  --shared-memory         Use shared linear memory
  --shared                Build a shared object
  --stack-first           Place stack at start of linear memory rather than after data
  --static                Alias for --Bstatic
  --strip-all             Strip all symbols
  --strip-debug           Strip debugging information
  -S                      Alias for --strip-debug
  -s                      Alias for --strip-all
  --thinlto-cache-dir=<value>
                          Path to ThinLTO cached object file directory
  --thinlto-cache-policy=<value>
                          Pruning policy for the ThinLTO cache
  --thinlto-jobs=<value>  Number of ThinLTO jobs. Default to --threads=
  --threads=<value>       Number of threads. '1' disables multi-threading. By default all available hardware thre
ads are used
  --trace-symbol=<value>  Trace references to symbols
  --trace                 Print the names of the input files
  -t                      Alias for --trace
  --undefined=<value>     Force undefined symbol during linking
  --unresolved-symbols=<value>
                          Determine how to handle unresolved symbols
  --verbose               Verbose mode
  --version               Display the version number and exit
  -v                      Display the version number
  --warn-unresolved-symbols
                          Report unresolved symbols as warnings
  --whole-archive         Force load of all members in a static library
  --wrap=<symbol>=<symbol>
                          Use wrapper functions for symbol
  -y <value>              Alias for --trace-symbol
  -z <option>             Linker option extensions
Fatal: Failed opening '/tmp/tinygo3144050016/main'  
usuyuki commented 10 months ago

なるほど,実質LLVMのwasmのオプションを直で叩いている感じっぽい? Go側に情報が書いてないのも納得.Rustも裏はLLVMなので,同じオプション行けそう.

usuyuki commented 10 months ago

でも指定で使っているz-stack-sizeとか無いんだが?

usuyuki commented 10 months ago

WebAssembly lld portのLLVMのページ https://lld.llvm.org/WebAssembly.html

ここと部分的に一致するが,stack-sizeとか無いものもある.

usuyuki commented 10 months ago

zstack-sizeの指定を消すと

tinygo:wasm-ld: error: initial memory too small, 66464 bytes needed  

となった.

usuyuki commented 10 months ago

-sを追加したら

❯ m r
tinygo build -target ./target.json -o main.wasm main.go
[wasm-validator error in function 11] unexpected false: Bulk memory operations require bulk memory [--enable-bulk-memory], on    
usuyuki commented 10 months ago

どこかでみたが,引数2つがだめという説もありそう.(スタックに入り切らない説) ただし同じスタックサイズの指定でRustでは動いているので,謎.(goはwasm32-unknonwn-wasiになっているから説が高い)