Skip to content

Commit 75504b5

Browse files
authored
Aggressive constprop in LinearAlgebra.wrap (#51582)
This helps with type-stability, as the flag `tA` is usually known from the type of the matrix. On master, ```julia julia> f(A) = LinearAlgebra.wrap(A, 'N') f (generic function with 1 method) julia> @code_typed f([1;;]) CodeInfo( 1 ─ %1 = invoke LinearAlgebra.wrap(A::Matrix{Int64}, 'N'::Char)::Union{Adjoint{Int64, Matrix{Int64}}, Hermitian{Int64, Matrix{Int64}}, Symmetric{Int64, Matrix{Int64}}, Transpose{Int64, Matrix{Int64}}, Matrix{Int64}} └── return %1 ) => Union{Adjoint{Int64, Matrix{Int64}}, Hermitian{Int64, Matrix{Int64}}, Symmetric{Int64, Matrix{Int64}}, Transpose{Int64, Matrix{Int64}}, Matrix{Int64}} ``` This PR ```julia julia> @code_typed f([1;;]) CodeInfo( 1 ─ return A ) => Matrix{Int64} ```
1 parent 2fab1b3 commit 75504b5

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/LinearAlgebra.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ wrapper_char(A::Hermitian) = A.uplo == 'U' ? 'H' : 'h'
465465
wrapper_char(A::Hermitian{<:Real}) = A.uplo == 'U' ? 'S' : 's'
466466
wrapper_char(A::Symmetric) = A.uplo == 'U' ? 'S' : 's'
467467

468-
function wrap(A::AbstractVecOrMat, tA::AbstractChar)
468+
Base.@constprop :aggressive function wrap(A::AbstractVecOrMat, tA::AbstractChar)
469469
if tA == 'N'
470470
return A
471471
elseif tA == 'T'

test/matmul.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ mul_wrappers = [
1717
m -> adjoint(m),
1818
m -> transpose(m)]
1919

20+
@testset "wrap" begin
21+
f(A) = LinearAlgebra.wrap(A, 'N')
22+
A = ones(1,1)
23+
@test @inferred(f(A)) === A
24+
g(A) = LinearAlgebra.wrap(A, 'T')
25+
@test @inferred(g(A)) === transpose(A)
26+
end
27+
2028
@testset "matrices with zero dimensions" begin
2129
for (dimsA, dimsB, dimsC) in (
2230
((0, 5), (5, 3), (0, 3)),

0 commit comments

Comments
 (0)