leanprover / leanprover/lean4

Improve code generation for `toCtorIdx`

Open
#2,033 1 comment 0 reactions 1 assignee View on GitHub

@zwarich is already working on this.

Since Mar 9, 2025.

code-generator P-low
Dominant language
Lean
Stars
9.2k
Forks
990
Avg merge
1d 17h
Merged PRs (30d)
175

Description

It should be possible to replace

@[extern c inline "(uint64_t)#1"]
def BinderInfo.toUInt64 : BinderInfo → UInt64
  | .default        => 0
  | .implicit       => 1
  | .strictImplicit => 2
  | .instImplicit   => 3

with

def BinderInfo.toUInt64 (bi : BinderInfo) : UInt64 :=
  bi.toCtorIdx.toUInt64

without pessimizing the generated code. However, this is not currently the case as toCtorIdx is compiled as a regular match and clang doesn't know enough about the domain of BinderInfo to get rid of those branches.

0000000001e0ec30 <l_Lean_BinderInfo_toUInt64>:
 1e0ec30:	85 ff                	test   %edi,%edi
 1e0ec32:	74 15                	je     1e0ec49 <l_Lean_BinderInfo_toUInt64+0x19>
 1e0ec34:	40 80 ff 02          	cmp    $0x2,%dil
 1e0ec38:	74 18                	je     1e0ec52 <l_Lean_BinderInfo_toUInt64+0x22>
 1e0ec3a:	40 80 ff 01          	cmp    $0x1,%dil
 1e0ec3e:	75 1b                	jne    1e0ec5b <l_Lean_BinderInfo_toUInt64+0x2b>
 1e0ec40:	b8 03 00 00 00       	mov    $0x3,%eax
 1e0ec45:	48 d1 e8             	shr    %rax
 1e0ec48:	c3                   	ret
 1e0ec49:	b8 01 00 00 00       	mov    $0x1,%eax
 1e0ec4e:	48 d1 e8             	shr    %rax
 1e0ec51:	c3                   	ret
 1e0ec52:	b8 05 00 00 00       	mov    $0x5,%eax
 1e0ec57:	48 d1 e8             	shr    %rax
 1e0ec5a:	c3                   	ret
 1e0ec5b:	b8 07 00 00 00       	mov    $0x7,%eax
 1e0ec60:	48 d1 e8             	shr    %rax
 1e0ec63:	c3                   	ret

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.