// File: rossy[P]crossc/SPIRV-Cross/spirv_msl.cpp
// Function: spirv_cross::CompilerMSL::emit_barrier(unsigned int, unsigned int, unsigned int)
void CompilerMSL::emit_barrier(uint32_t id_exe_scope, uint32_t id_mem_scope, uint32_t id_mem_sem)
{
	if (get_entry_point().model != ExecutionModelGLCompute)
		return;

	string bar_stmt = "threadgroup_barrier(mem_flags::";

	uint32_t mem_sem = id_mem_sem ? get<SPIRConstant>(id_mem_sem).scalar() : uint32_t(MemorySemanticsMaskNone);

	if (mem_sem & MemorySemanticsCrossWorkgroupMemoryMask)
		bar_stmt += "mem_device";
	else if (mem_sem & (MemorySemanticsSubgroupMemoryMask | MemorySemanticsWorkgroupMemoryMask |
	                    MemorySemanticsAtomicCounterMemoryMask))
		bar_stmt += "mem_threadgroup";
	else if (mem_sem & MemorySemanticsImageMemoryMask)
		bar_stmt += "mem_texture";
	else
		bar_stmt += "mem_none";

	if (msl_options.is_ios() && msl_options.supports_msl_version(2))
	{
		bar_stmt += ", ";

		// Use the wider of the two scopes (smaller value)
		uint32_t exe_scope = id_exe_scope ? get<SPIRConstant>(id_exe_scope).scalar() : uint32_t(ScopeInvocation);
		uint32_t mem_scope = id_mem_scope ? get<SPIRConstant>(id_mem_scope).scalar() : uint32_t(ScopeInvocation);
		uint32_t scope = min(exe_scope, mem_scope);
		switch (scope)
		{
		case ScopeCrossDevice:
		case ScopeDevice:
			bar_stmt += "memory_scope_device";
			break;

		case ScopeSubgroup:
		case ScopeInvocation:
			bar_stmt += "memory_scope_simdgroup";
			break;

		case ScopeWorkgroup:
		default:
			bar_stmt += "memory_scope_threadgroup";
			break;
		}
	}

	bar_stmt += ");";

	statement(bar_stmt);

	assert(current_emitting_block);
	flush_control_dependent_expressions(current_emitting_block->self);
	flush_all_active_variables();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl %ecx, -0x14(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x68(%rbp)
callq 0x25150
cmpl $0x5, 0xb8(%rax)
je 0xff093
jmp 0xff40d
leaq -0x39(%rbp), %rdi
movq %rdi, -0x70(%rbp)
callq 0x7650
movq -0x70(%rbp), %rdx
leaq 0x4db1a(%rip), %rsi     # 0x14cbc5
leaq -0x38(%rbp), %rdi
callq 0x7520
jmp 0xff0b6
leaq -0x39(%rbp), %rdi
callq 0x73d0
cmpl $0x0, -0x14(%rbp)
je 0xff0f1
movq -0x68(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x31910
movq %rax, -0x78(%rbp)
jmp 0xff0d7
movq -0x78(%rbp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x2f560
movl %eax, -0x7c(%rbp)
jmp 0xff0e9
movl -0x7c(%rbp), %eax
movl %eax, -0x80(%rbp)
jmp 0xff0f8
xorl %eax, %eax
movl %eax, -0x80(%rbp)
jmp 0xff0f8
movl -0x80(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x50(%rbp), %eax
andl $0x200, %eax            # imm = 0x200
cmpl $0x0, %eax
je 0xff156
leaq 0x4dad3(%rip), %rsi     # 0x14cbe5
leaq -0x38(%rbp), %rdi
callq 0x7340
jmp 0xff11d
jmp 0xff1ee
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x39(%rbp), %rdi
callq 0x73d0
jmp 0xff416
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x7708
jmp 0xff416
movl -0x50(%rbp), %eax
movl %eax, -0x88(%rbp)
movl $0x80, %edi
movl $0x100, %esi            # imm = 0x100
callq 0xa6820
movl %eax, -0x84(%rbp)
jmp 0xff176
movl -0x84(%rbp), %edi
movl $0x400, %esi            # imm = 0x400
callq 0xa6820
movl %eax, -0x8c(%rbp)
jmp 0xff18e
movl -0x8c(%rbp), %ecx
movl -0x88(%rbp), %eax
andl %ecx, %eax
cmpl $0x0, %eax
je 0xff1b5
leaq 0x4da48(%rip), %rsi     # 0x14cbf0
leaq -0x38(%rbp), %rdi
callq 0x7340
jmp 0xff1b3
jmp 0xff1ec
movl -0x50(%rbp), %eax
andl $0x800, %eax            # imm = 0x800
cmpl $0x0, %eax
je 0xff1d6
leaq 0x4da37(%rip), %rsi     # 0x14cc00
leaq -0x38(%rbp), %rdi
callq 0x7340
jmp 0xff1d4
jmp 0xff1ea
leaq 0x4da2f(%rip), %rsi     # 0x14cc0c
leaq -0x38(%rbp), %rdi
callq 0x7340
jmp 0xff1e8
jmp 0xff1ea
jmp 0xff1ec
jmp 0xff1ee
movq -0x68(%rbp), %rdi
addq $0x7cc, %rdi            # imm = 0x7CC
callq 0x116b00
movb %al, -0x8d(%rbp)
jmp 0xff206
movb -0x8d(%rbp), %al
testb $0x1, %al
jne 0xff215
jmp 0xff394
movq -0x68(%rbp), %rdi
addq $0x7cc, %rdi            # imm = 0x7CC
movl $0x2, %esi
xorl %ecx, %ecx
movl %ecx, %edx
callq 0x1160d0
movb %al, -0x8e(%rbp)
jmp 0xff236
movb -0x8e(%rbp), %al
testb $0x1, %al
jne 0xff245
jmp 0xff394
leaq 0x45343(%rip), %rsi     # 0x14458f
leaq -0x38(%rbp), %rdi
callq 0x7340
jmp 0xff257
cmpl $0x0, -0xc(%rbp)
je 0xff298
movq -0x68(%rbp), %rdi
movl -0xc(%rbp), %esi
callq 0x31910
movq %rax, -0x98(%rbp)
jmp 0xff272
movq -0x98(%rbp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x2f560
movl %eax, -0x9c(%rbp)
jmp 0xff28a
movl -0x9c(%rbp), %eax
movl %eax, -0xa0(%rbp)
jmp 0xff2a5
movl $0x4, %eax
movl %eax, -0xa0(%rbp)
jmp 0xff2a5
movl -0xa0(%rbp), %eax
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x10(%rbp)
je 0xff2ef
movq -0x68(%rbp), %rdi
movl -0x10(%rbp), %esi
callq 0x31910
movq %rax, -0xa8(%rbp)
jmp 0xff2c9
movq -0xa8(%rbp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x2f560
movl %eax, -0xac(%rbp)
jmp 0xff2e1
movl -0xac(%rbp), %eax
movl %eax, -0xb0(%rbp)
jmp 0xff2fc
movl $0x4, %eax
movl %eax, -0xb0(%rbp)
jmp 0xff2fc
movl -0xb0(%rbp), %eax
movl %eax, -0x58(%rbp)
leaq -0x54(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0xa0900
movq %rax, -0xb8(%rbp)
jmp 0xff31b
movq -0xb8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
movl %eax, -0xbc(%rbp)
subl $0x2, %eax
jb 0xff354
jmp 0xff337
movl -0xbc(%rbp), %eax
subl $0x2, %eax
je 0xff37c
jmp 0xff344
movl -0xbc(%rbp), %eax
addl $-0x3, %eax
subl $0x2, %eax
jb 0xff368
jmp 0xff37e
leaq 0x4d8ba(%rip), %rsi     # 0x14cc15
leaq -0x38(%rbp), %rdi
callq 0x7340
jmp 0xff366
jmp 0xff392
leaq 0x4d8ba(%rip), %rsi     # 0x14cc29
leaq -0x38(%rbp), %rdi
callq 0x7340
jmp 0xff37a
jmp 0xff392
jmp 0xff37e
leaq 0x4d8bb(%rip), %rsi     # 0x14cc40
leaq -0x38(%rbp), %rdi
callq 0x7340
jmp 0xff390
jmp 0xff392
jmp 0xff394
leaq 0x49d0a(%rip), %rsi     # 0x1490a5
leaq -0x38(%rbp), %rdi
callq 0x7340
jmp 0xff3a6
movq -0x68(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x10200
jmp 0xff3b5
movq -0x68(%rbp), %rax
cmpq $0x0, 0x410(%rax)
je 0xff3c5
jmp 0xff3e4
leaq 0x44d8b(%rip), %rdi     # 0x144157
leaq 0x4b6ec(%rip), %rsi     # 0x14aabf
movl $0x9e7, %edx            # imm = 0x9E7
leaq 0x4d87a(%rip), %rcx     # 0x14cc59
callq 0x7260
movq -0x68(%rbp), %rdi
movq 0x410(%rdi), %rax
movl 0x8(%rax), %esi
callq 0x20030
jmp 0xff3f9
movq -0x68(%rbp), %rdi
callq 0x200e0
jmp 0xff404
leaq -0x38(%rbp), %rdi
callq 0x7708
addq $0xc0, %rsp
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x7640
nop