tool-conventions icon indicating copy to clipboard operation
tool-conventions copied to clipboard

performance issue for std::span on wasm C calling convention

Open trcrsired opened this issue 2 years ago • 5 comments

#include<span>
#include<cstddef>

void baz(::std::size_t *ptr,::std::size_t n) noexcept;

void foo(::std::span<::std::size_t> sp) noexcept
{
	baz(sp.data(),sp.size());
}

void bar(::std::size_t *ptr,::std::size_t n) noexcept
{
	baz(ptr,n);
}
_Z3fooNSt3__14spanImLm4294967295EEE:    # @_Z3fooNSt3__14spanImLm4294967295EEE
	.functype	_Z3fooNSt3__14spanImLm4294967295EEE (i32) -> ()
# %bb.0:
	local.get	0
	i32.load	0
	local.get	0
	i32.load	4
	call	_Z3bazPmm
                                        # fallthrough-return
	end_function
                                        # -- End function
	.section	.text._Z3barPmm,"",@
	.hidden	_Z3barPmm                       # -- Begin function _Z3barPmm
	.globl	_Z3barPmm
	.type	_Z3barPmm,@function
_Z3barPmm:                              # @_Z3barPmm
	.functype	_Z3barPmm (i32, i32) -> ()
# %bb.0:
	local.get	0
	local.get	1
	call	_Z3bazPmm
                                        # fallthrough-return
	end_function

std::span is passed by memory, not by registers on wasm. which is extremely slow.

This is going to encourage people to overuse pointers. Please change the calling convention here. Also, the return value has the same issue.

trcrsired avatar Oct 03 '23 04:10 trcrsired

https://developercommunity.visualstudio.com/t/I-present-a-novel-calling-convention-nam/10433601?q=wincall

trcrsired avatar Oct 03 '23 04:10 trcrsired

What happens if you enable -mmulti-value? IIRC we use a different calling convention there, which passes structures in registers.

dschuff avatar Oct 03 '23 15:10 dschuff

What happens if you enable -mmulti-value? IIRC we use a different calling convention there, which passes structures in registers.

Nothing changes. It is an ABI issue.

trcrsired avatar Oct 26 '23 23:10 trcrsired

The full set of magic flags @dschuff is thinking of is -mmultivalue -Xclang -target-abi -Xclang experimental-mv.

tlively avatar Oct 27 '23 01:10 tlively

Ah thanks for that, I had forgotten that the feature enabler and the ABI were separate flags.

dschuff avatar Oct 27 '23 15:10 dschuff