Skip to content

Commit

Permalink
Add githook that blocks commits that aren't formatted
Browse files Browse the repository at this point in the history
Fixes #48
  • Loading branch information
markehammons committed Feb 7, 2023
1 parent 695635f commit f43c6c9
Show file tree
Hide file tree
Showing 51 changed files with 270 additions and 213 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jobs:
java-version: |
17
19
- name: Formatting Check
run: ./mill mill.scalalib.scalafmt.ScalafmtModule/checkFormatAll __.sources
- name: Core tests
run: |
./mill core.compile
Expand Down
2 changes: 1 addition & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
version = "3.6.1"
version = "3.7.1"
runner.dialect = scala3
indent.main = 2
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fr.hammons.slinc

import org.openjdk.jmh.annotations.*,
Mode.{SampleTime, SingleShotTime, Throughput}
Mode.{SampleTime, SingleShotTime, Throughput}
import java.util.concurrent.TimeUnit
import fr.hammons.slinc.Scope
import scala.util.Random
Expand Down Expand Up @@ -33,7 +33,7 @@ trait BindingsBenchmarkShape(val s: Slinc):

val base = Seq.fill(10000)(Random.nextInt)
val baseArr = base.toArray

val upcall: Ptr[(Ptr[Int], Ptr[Int]) => Int] = Scope.global {
Ptr.upcall((a, b) =>
val aVal = !a
Expand All @@ -49,11 +49,11 @@ trait BindingsBenchmarkShape(val s: Slinc):
Cstd.abs(6)

@Benchmark
def labs =
def labs =
Cstd.labs(-15.as[CLong])

@Benchmark
def labs2 =
def labs2 =
Cstd2.labs(-15)

@Benchmark
Expand Down Expand Up @@ -84,5 +84,3 @@ trait BindingsBenchmarkShape(val s: Slinc):
@OutputTimeUnit(TimeUnit.MILLISECONDS)
def scalasort =
baseArr.sorted


Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fr.hammons.slinc

import org.openjdk.jmh.annotations.*,
Mode.{SampleTime, SingleShotTime, Throughput}
Mode.{SampleTime, SingleShotTime, Throughput}
import java.util.concurrent.TimeUnit
import fr.hammons.slinc.Scope
import scala.annotation.experimental
Expand All @@ -19,47 +19,42 @@ trait TransferBenchmarkShape(val s: Slinc):
given Struct[C] = Struct.derived
given Struct[D] = Struct.derived

val aPtr = Scope.global{
val aPtr = Scope.global {
Ptr.blank[A]
}

val a = A(1,B(2,3),4)
val a = A(1, B(2, 3), 4)

val cPtr = Scope.global{
val cPtr = Scope.global {
Ptr.blank[C]
}

val c = C(1,D(2,3),4)
val c = C(1, D(2, 3), 4)

@Benchmark
def topLevelRead =
@Benchmark
def topLevelRead =
!aPtr

@Benchmark
@Benchmark
def topLevelWrite =
!aPtr = a


@Benchmark
def innerRead =
!cPtr
!cPtr

@Benchmark
@Benchmark
def innerWrite =
!cPtr = c

@Benchmark
def allocateFnPointer =
def allocateFnPointer =
Scope.confined(
Ptr.upcall((a: Ptr[Int]) => !a + 1)
)


@Benchmark
@Benchmark
def allocateIntPointer =
Scope.confined(
Ptr.copy(3)
)



2 changes: 1 addition & 1 deletion core/src/fr/hammons/slinc/Bytes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ object Bytes:
extension (a: Bytes)
inline def +(b: Bytes): Bytes = a + b
inline def *(i: Int): Bytes = a * i
inline def *(b: Bytes): Bytes = a * b
inline def *(b: Bytes): Bytes = a * b
inline def %(b: Bytes): Bytes = a % b
inline def -(b: Bytes): Bytes = a - b
inline def toLong: Long = a
Expand Down
24 changes: 14 additions & 10 deletions core/src/fr/hammons/slinc/DescriptorOf.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ import fr.hammons.slinc.container.*
import scala.quoted.*

/** Typeclass that summons TypeDescriptors
*
*/
trait DescriptorOf[A]:
val descriptor: TypeDescriptor

object DescriptorOf:
/** Convenience method for summoning the TypeDescriptor attached to DescriptorOf.
*
* @example DescriptorOf[Int] //returns IntDescriptor
/** Convenience method for summoning the TypeDescriptor attached to
* DescriptorOf.
*
* @example
* DescriptorOf[Int] //returns IntDescriptor
*
* @param d
* @return TypeDescriptor for type A
* @return
* TypeDescriptor for type A
*/
def apply[A](using d: DescriptorOf[A]) = d.descriptor

Expand All @@ -42,7 +44,7 @@ object DescriptorOf:
given DescriptorOf[Double] with
val descriptor: TypeDescriptor = DoubleDescriptor

//this is the general DescriptorOf for all [[Ptr[A]]]
// this is the general DescriptorOf for all [[Ptr[A]]]
private val ptrDescriptor = new DescriptorOf[Ptr[?]]:
val descriptor: TypeDescriptor = PtrDescriptor

Expand All @@ -51,8 +53,10 @@ object DescriptorOf:

def getDescriptorFor[A](using Quotes, Type[A]) =
import quotes.reflect.*
val expr = Expr.summon[DescriptorOf[A]].getOrElse(
report.errorAndAbort(s"Cannot find a descriptor of ${Type.show[A]}")
)
val expr = Expr
.summon[DescriptorOf[A]]
.getOrElse(
report.errorAndAbort(s"Cannot find a descriptor of ${Type.show[A]}")
)

'{$expr.descriptor}
'{ $expr.descriptor }
2 changes: 1 addition & 1 deletion core/src/fr/hammons/slinc/DowncallI.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class DowncallI(platform: DowncallI.PlatformSpecific):
getDowncallHelper[T],
summonFrom {
case dO: DescriptorOf[R] => Some(dO.descriptor)
case _ => None
case _ => None
}
)

Expand Down
30 changes: 21 additions & 9 deletions core/src/fr/hammons/slinc/LibraryI.scala
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ object LibraryI:
val owningClass = MacroHelpers.findOwningClass(Symbol.spliceOwner)
val library = LibraryI.getLibrary[L](owningClass)


val methodSymbol = MacroHelpers.findOwningMethod(Symbol.spliceOwner)
val methodPositionExpr = MacroHelpers
.getMethodSymbols(owningClass)
Expand Down Expand Up @@ -214,16 +213,28 @@ object LibraryI:
)
}
report.info(
s"""|Binding doesn't need allocator: ${allocationlessInputs == mappedInputs && !needsAllocator(methodSymbol)}
|Binding has return that requires allocator: ${needsAllocator(methodSymbol)}
s"""|Binding doesn't need allocator: ${allocationlessInputs == mappedInputs && !needsAllocator(
methodSymbol
)}
|Binding has return that requires allocator: ${needsAllocator(
methodSymbol
)}
|Binding has inputs that require allocation: ${allocationlessInputs != mappedInputs}
|Allocationless inputs: ${allocationlessInputs.map(_.asTerm.show(using Printer.TreeShortCode))}
|Mapped inputs: ${mappedInputs.map(_.asTerm.show(using Printer.TreeShortCode))}
|Generated code: ${code.asTerm.show(using Printer.TreeShortCode)}""".stripMargin
|Allocationless inputs: ${allocationlessInputs.map(
_.asTerm.show(using Printer.TreeShortCode)
)}
|Mapped inputs: ${mappedInputs.map(
_.asTerm.show(using Printer.TreeShortCode)
)}
|Generated code: ${code.asTerm.show(using
Printer.TreeShortCode
)}""".stripMargin
)
code

def getLibrary[L[_]](using q: Quotes)(using Type[L])(owningClass: q.reflect.Symbol): Expr[L[Any]] =
def getLibrary[L[_]](using q: Quotes)(using Type[L])(
owningClass: q.reflect.Symbol
): Expr[L[Any]] =
import quotes.reflect.*

TypeRepr.of[L].appliedTo(owningClass.typeRef).asType match
Expand All @@ -245,13 +256,14 @@ object LibraryI:
import quotes.reflect.*
val name: LibraryLocation = LibraryName.libraryName[L]
name match
case LibraryLocation.Standardard => '{ $platformSpecificExpr.getStandardLibLookup }
case LibraryLocation.Standardard =>
'{ $platformSpecificExpr.getStandardLibLookup }
case LibraryLocation.Local(s) =>
'{ $platformSpecificExpr.getLocalLookup(${ Expr(s) }) }
case LibraryLocation.Path(s) =>
'{ $platformSpecificExpr.getLibraryPathLookup(${ Expr(s) }) }
case LibraryLocation.Resource(s) =>
'{ $platformSpecificExpr.getResourceLibLookup(${Expr(s)} ) }
'{ $platformSpecificExpr.getResourceLibLookup(${ Expr(s) }) }

inline def getMethodAddress[L](l: Lookup) = ${
getMethodAddressImpl[L]('l)
Expand Down
26 changes: 15 additions & 11 deletions core/src/fr/hammons/slinc/LibraryName.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@ object LibraryName:
def libraryName[L](using Quotes, Type[L]) =
import quotes.reflect.*
val classSymbol = MacroHelpers.getClassSymbol[L]
classSymbol.annotations.collect {
case Apply(
Select(New(TypeIdent("LibraryName")), "<init>"),
List(Literal(StringConstant(name)))
) =>
name
}.map{
case s"@$path" => LibraryLocation.Resource(path)
case s"#$path" => LibraryLocation.Path(path)
case s => LibraryLocation.Local(s)
}.headOption.getOrElse(LibraryLocation.Standardard)
classSymbol.annotations
.collect {
case Apply(
Select(New(TypeIdent("LibraryName")), "<init>"),
List(Literal(StringConstant(name)))
) =>
name
}
.map {
case s"@$path" => LibraryLocation.Resource(path)
case s"#$path" => LibraryLocation.Path(path)
case s => LibraryLocation.Local(s)
}
.headOption
.getOrElse(LibraryLocation.Standardard)
15 changes: 9 additions & 6 deletions core/src/fr/hammons/slinc/Lookup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ trait Lookup(libraryLocation: LibraryLocation):
def lookup(name: String): Object

def lookupError(name: String): Error = libraryLocation match
case LibraryLocation.Standardard => Error(s"Failed to load symbol $name from the standard library.")
case LibraryLocation.Resource(location) =>
Error(s"Failed to load symbol $name from resource $location. This could be caused by resource collision. Is the resource name unique enough?")
case LibraryLocation.Local(location) =>
case LibraryLocation.Standardard =>
Error(s"Failed to load symbol $name from the standard library.")
case LibraryLocation.Resource(location) =>
Error(
s"Failed to load symbol $name from resource $location. This could be caused by resource collision. Is the resource name unique enough?"
)
case LibraryLocation.Local(location) =>
val absPath = Paths.get(location).nn.toAbsolutePath.nn
Error(s"Failed to load symbol $name from local path $absPath")
case LibraryLocation.Path(libName) =>
Error(s"Failed to load symbol $name from library $libName")
case LibraryLocation.Path(libName) =>
Error(s"Failed to load symbol $name from library $libName")
2 changes: 1 addition & 1 deletion core/src/fr/hammons/slinc/Mem.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ trait Mem:
def readLong(offset: Bytes): Long
def readDouble(offset: Bytes): Double

def readIntArray(offset: Bytes, size: Int): Array[Int]
def readIntArray(offset: Bytes, size: Int): Array[Int]
7 changes: 5 additions & 2 deletions core/src/fr/hammons/slinc/MethodHandleTools.scala
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ object MethodHandleTools:
def calculateMethodHandleImplementation[L](
platformExpr: Expr[LibraryI.PlatformSpecific],
addresses: Expr[IArray[Object]]
)(using Quotes, Type[L]): Expr[(IArray[MethodHandle], IArray[Seq[TypeDescriptor] => MethodHandle])] =
)(using
Quotes,
Type[L]
): Expr[(IArray[MethodHandle], IArray[Seq[TypeDescriptor] => MethodHandle])] =
import quotes.reflect.*

val methodSymbols = MacroHelpers.getMethodSymbols(
Expand Down Expand Up @@ -168,7 +171,7 @@ object MethodHandleTools:
inline def calculateMethodHandles[L](
platformSpecific: LibraryI.PlatformSpecific,
addresses: IArray[Object]
): (IArray[MethodHandle], IArray[Seq[TypeDescriptor] => MethodHandle]) = ${
): (IArray[MethodHandle], IArray[Seq[TypeDescriptor] => MethodHandle]) = ${
calculateMethodHandleImplementation[L]('platformSpecific, 'addresses)
}

Expand Down
4 changes: 3 additions & 1 deletion core/src/fr/hammons/slinc/Ptr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class Ptr[A](private[slinc] val mem: Mem, private[slinc] val offset: Bytes):

object Ptr:
extension (p: Ptr[Byte])
def copyIntoString(maxSize: Int)(using DescriptorOf[Byte], DescriptorModule) =
def copyIntoString(
maxSize: Int
)(using DescriptorOf[Byte], DescriptorModule) =
var i = 0
val resizedPtr = p.resize(Bytes(maxSize))
while (i < maxSize && !resizedPtr(i) != 0) do i += 1
Expand Down
3 changes: 2 additions & 1 deletion core/src/fr/hammons/slinc/Slinc.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ trait Slinc:

export types.os

def sizeOf[A](using l: DescriptorOf[A]) = DescriptorOf[A].size.toLong.maybeAs[SizeT]
def sizeOf[A](using l: DescriptorOf[A]) =
DescriptorOf[A].size.toLong.maybeAs[SizeT]

def Null[A] = scopePlatformSpecific.nullPtr[A]

Expand Down
15 changes: 9 additions & 6 deletions core/src/fr/hammons/slinc/Struct.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ class StructI(
jitManager: JitManager
)(using DescriptorModule):
/** Summons up Descriptors for the members of Product A
*
*
* @tparam A The product type to summon a list of descriptors for
* @return List[TypeDescriptor]
* @tparam A
* The product type to summon a list of descriptors for
* @return
* List[TypeDescriptor]
*/
private inline def memberDescriptors[A](using m: Mirror.ProductOf[A]): List[TypeDescriptor] =
private inline def memberDescriptors[A](using
m: Mirror.ProductOf[A]
): List[TypeDescriptor] =
memberDescriptorsHelper[m.MirroredElemTypes]
private inline def memberDescriptorsHelper[T <: Tuple]: List[TypeDescriptor] =
inline erasedValue[T] match
Expand All @@ -30,9 +33,9 @@ class StructI(
case _: EmptyTuple => Nil

/** Summons up the names of members of the Product A
*
*
* @tparam A A product type representing a C struct.
* @tparam A
* A product type representing a C struct.
* @return
*/
private inline def memberNames[A](using m: Mirror.ProductOf[A]) =
Expand Down
3 changes: 2 additions & 1 deletion core/src/fr/hammons/slinc/Tools.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ object Tools:
cacheLocation.nn.toAbsolutePath().nn.toString(),
headerLocation.nn.toAbsolutePath().nn.toString()
)
if cmd.! != 0 then throw Error(s"failed to compile $headerLocation: ${cmd.mkString(" ")}")
if cmd.! != 0 then
throw Error(s"failed to compile $headerLocation: ${cmd.mkString(" ")}")

def loadCachedLibrary(name: String) =
val cacheLocation = appDataStore.resolve(s"$name$sharedLibSuffix")
Expand Down
Loading

0 comments on commit f43c6c9

Please sign in to comment.