Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add language: Mojo #6400

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,9 @@
[submodule "vendor/grammars/mlir-grammar"]
path = vendor/grammars/mlir-grammar
url = https://github.com/jpienaar/mlir-grammar
[submodule "vendor/grammars/mojo-syntax"]
path = vendor/grammars/mojo-syntax
url = https://github.com/modularml/mojo-syntax.git
[submodule "vendor/grammars/monkey"]
path = vendor/grammars/monkey
url = https://github.com/gingerbeardman/monkey.tmbundle
Expand Down
2 changes: 2 additions & 0 deletions grammars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,8 @@ vendor/grammars/mint-vscode:
- source.mint
vendor/grammars/mlir-grammar:
- source.mlir
vendor/grammars/mojo-syntax:
- source.mojo
vendor/grammars/monkey:
- source.monkey
vendor/grammars/moonscript-tmbundle:
Expand Down
6 changes: 6 additions & 0 deletions lib/linguist/heuristics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,12 @@ disambiguations:
- language: Modula-2
pattern: '^\s*(?i:MODULE|END) [\w\.]+;'
- language: [Linux Kernel Module, AMPL]
- extensions: ['.mojo']
rules:
- language: Mojo
pattern: '^\s*(alias|def|from|fn|import|struct|trait)\s'
- language: XML
pattern: '^\s*<\?xml'
- extensions: ['.ms']
rules:
- language: Roff
Expand Down
11 changes: 11 additions & 0 deletions lib/linguist/languages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4329,6 +4329,16 @@ Module Management System:
tm_scope: none
ace_mode: text
language_id: 235
Mojo:
type: programming
color: "#ff4c1f"
extensions:
- ".mojo"
ace_mode: python
codemirror_mode: python
codemirror_mime_type: text/x-python
tm_scope: source.mojo
language_id: 1045019587
Monkey:
type: programming
extensions:
Expand Down Expand Up @@ -7854,6 +7864,7 @@ XML:
- ".mjml"
- ".mm"
- ".mod"
- ".mojo"
- ".mxml"
- ".natvis"
- ".ncl"
Expand Down
31 changes: 31 additions & 0 deletions samples/Mojo/Bool.mojo
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
alias OurTrue = OurBool(__mlir_attr.`true`)
alias OurFalse: OurBool = __mlir_attr.`false`


@register_passable("trivial")
struct OurBool:
var value: __mlir_type.i1

fn __init__() -> Self:
return OurFalse

fn __init__(value: __mlir_type.i1) -> Self:
return Self {value: value}

fn __bool__(self) -> Bool:
return Bool(self.value)

fn __mlir_i1__(self) -> __mlir_type.i1:
return self.value

fn __eq__(self, rhs: OurBool) -> Self:
let lhsIndex = __mlir_op.`index.casts`[_type : __mlir_type.index](self.value)
let rhsIndex = __mlir_op.`index.casts`[_type : __mlir_type.index](rhs.value)
return Self(
__mlir_op.`index.cmp`[pred : __mlir_attr.`#index<cmp_predicate eq>`](
lhsIndex, rhsIndex
)
)

fn __invert__(self) -> Self:
return OurFalse if self == OurTrue else OurTrue
152 changes: 152 additions & 0 deletions samples/Mojo/Matmul.mojo
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
from autotune import autotune, search
from algorithm import parallelize, Static2DTileUnitFunc as Tile2DFunc, vectorize_unroll
from benchmark import Benchmark
from sys.intrinsics import strided_load
from utils.list import VariadicList
from math import div_ceil, min
from memory import memset_zero
from memory.unsafe import DTypePointer, Pointer
from random import rand, random_float64
from sys.info import simdwidthof
from time import now


struct Matrix:
var data: DTypePointer[DType.float32]
var rows: Int
var cols: Int

fn __init__(inout self, rows: Int, cols: Int):
self.data = DTypePointer[DType.float32].alloc(rows * cols)
rand(self.data, rows * cols)
self.rows = rows
self.cols = cols

fn __del__(owned self):
self.data.free()

fn zero(inout self):
memset_zero(self.data, self.rows * self.cols)

@always_inline
fn __getitem__(self, y: Int, x: Int) -> Float32:
return self.load[1](y, x)

@always_inline
fn load[nelts: Int](self, y: Int, x: Int) -> SIMD[DType.float32, nelts]:
return self.data.simd_load[nelts](y * self.cols + x)

@always_inline
fn __setitem__(self, y: Int, x: Int, val: Float32):
return self.store[1](y, x, val)

@always_inline
fn store[nelts: Int](self, y: Int, x: Int, val: SIMD[DType.float32, nelts]):
self.data.simd_store[nelts](y * self.cols + x, val)


alias matmul_fn_sig_type = fn (Matrix, Matrix, Matrix) -> None


# Perform 2D tiling on the iteration space defined by end_x and end_y.
fn tile[tiled_fn: Tile2DFunc, tile_x: Int, tile_y: Int](end_x: Int, end_y: Int):
# Note: this assumes that ends are multiples of the tiles.
for y in range(0, end_y, tile_y):
for x in range(0, end_x, tile_x):
tiled_fn[tile_x, tile_y](x, y)


alias nelts = simdwidthof[DType.float32]() # The SIMD vector width.


# Autotune the tile size used in the matmul.
@adaptive
fn matmul_autotune_impl(C: Matrix, A: Matrix, B: Matrix):
@parameter
fn calc_row(m: Int):
@parameter
fn calc_tile[tile_x: Int, tile_y: Int](x: Int, y: Int):
for k in range(y, y + tile_y):

@parameter
fn dot[
nelts: Int,
](n: Int):
C.store[nelts](
m,
n + x,
C.load[nelts](m, n + x) + A[m, k] * B.load[nelts](k, n + x),
)

vectorize_unroll[nelts, tile_x // nelts, dot](tile_x)

# Instead of hardcoding to tile_size = 4, search for the fastest
# tile size by evaluting this function as tile size varies.
alias tile_size = autotune(1, 2, 4, 8, 16, 32)
tile[calc_tile, nelts * tile_size, tile_size](A.cols, C.cols)

parallelize[calc_row](C.rows)


fn matmul_evaluator(funcs: Pointer[matmul_fn_sig_type], size: Int) -> Int:
print("matmul_evaluator, number of candidates: ", size)

let eval_begin: Int = now()

# This size is picked at random, in real code we could use a real size
# distribution here.
let M = 512
let N = 512
let K = 512
print("Optimizing for size:", M, "x", N, "x", K)

var best_idx: Int = -1
var best_time: Int = -1

alias eval_iterations = 10
alias eval_samples = 10

var C = Matrix(M, N)
var A = Matrix(M, K)
var B = Matrix(K, N)
let Cptr = Pointer[Matrix].address_of(C).address
let Aptr = Pointer[Matrix].address_of(A).address
let Bptr = Pointer[Matrix].address_of(B).address

# Find the function that's the fastest on the size we're optimizing for
for f_idx in range(size):
let func = funcs.load(f_idx)

@always_inline
@parameter
fn wrapper():
func(C, A, B)

let cur_time = Benchmark(1, 100_000, 500_000_000, 1000_000_000).run[wrapper]()

if best_idx < 0:
best_idx = f_idx
best_time = cur_time
if best_time > cur_time:
best_idx = f_idx
best_time = cur_time

let eval_end: Int = now()
# Prevent matrices from being destroyed before we finished benchmarking them.
_ = A.data
_ = B.data
_ = C.data
print("Time spent in matmul_evaluator, ms:", (eval_end - eval_begin) // 1000000)
print("Best candidate idx:", best_idx)
return best_idx


fn matmul_autotune(C: Matrix, A: Matrix, B: Matrix):
alias best_impl: matmul_fn_sig_type
search[
matmul_fn_sig_type,
VariadicList(matmul_autotune_impl.__adaptive_set),
matmul_evaluator -> best_impl
]()
# Run the best candidate
return best_impl(C, A, B)
3 changes: 3 additions & 0 deletions samples/XML/Example.mojo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="Example" board="Mojo V3" language="Lucid">
</project>
7 changes: 7 additions & 0 deletions test/test_heuristics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,13 @@ def test_mod_by_heuristics
})
end

def test_mojo_by_heuristics
assert_heuristics({
"Mojo" => all_fixtures("Mojo", "*.mojo"),
"XML" => all_fixtures("XML", "*.mojo"),
})
end

def test_ms_by_heuristics
assert_heuristics({
"Roff" => all_fixtures("Roff", "*.ms"),
Expand Down
1 change: 1 addition & 0 deletions vendor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ This is a list of grammars that Linguist selects to provide syntax highlighting
- **Modelica:** [BorisChumichev/modelicaSublimeTextPackage](https://github.com/BorisChumichev/modelicaSublimeTextPackage)
- **Modula-2:** [harogaston/Sublime-Modula-2](https://github.com/harogaston/Sublime-Modula-2)
- **Modula-3:** [newgrammars/m3](https://github.com/newgrammars/m3)
- **Mojo:** [modularml/mojo-syntax](https://github.com/modularml/mojo-syntax)
- **Monkey:** [gingerbeardman/monkey.tmbundle](https://github.com/gingerbeardman/monkey.tmbundle)
- **Monkey C:** [ghisguth/vscode-monkey-c](https://github.com/ghisguth/vscode-monkey-c)
- **MoonScript:** [leafo/moonscript-tmbundle](https://github.com/leafo/moonscript-tmbundle)
Expand Down
1 change: 1 addition & 0 deletions vendor/grammars/mojo-syntax
Submodule mojo-syntax added at a2241d
33 changes: 33 additions & 0 deletions vendor/licenses/git_submodule/mojo-syntax.dep.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
name: mojo-syntax
version: a2241dda06d9a20503394c3affa606114d007049
type: git_submodule
homepage: https://github.com/modularml/mojo-syntax.git
license: mit
licenses:
- sources: LICENSE
text: |
The MIT License

Copyright (c) 2022-present Modular Inc. https://www.modular.com
Copyright (c) 2015-present MagicStack Inc. http://magic.io
Copyright (c) 2015-present Microsoft Corporation

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
notices: []
Loading