From 21b072ab18d0e45022f674d3646f32b4f29a3421 Mon Sep 17 00:00:00 2001 From: Mamy Ratsimbazafy Date: Sun, 21 Jul 2024 01:08:14 +0200 Subject: [PATCH 1/2] fix(nvidia): reorg + rename following #402 --- .../hashes/sha256/sha256_x86_shaext.nim | 2 +- .../hashes/sha256/sha256_x86_ssse3.nim | 2 +- constantine/math/extension_fields/towers.nim | 4 ++-- .../codegen_nvidia.nim} | 10 ++++----- ...elds_nvidia.nim => impl_fields_nvidia.nim} | 3 ++- .../code_generator => math_compiler}/ir.nim | 6 ++--- .../{code_generator => llvm}/README.md | 2 +- .../bindings/c_abi.nim | 0 .../bindings/llvm_abi.nim | 0 .../bindings/nvidia_abi.nim | 0 .../{code_generator => llvm}/llvm.nim | 0 .../nvidia_inlineasm.nim | 0 .../platforms/metering/benchmarking.nim | 2 +- constantine/platforms/primitives.nim | 2 +- .../platforms/{isa => x86}/cpudetect_x86.nim | 0 .../{isa => x86}/macro_assembler_x86.nim | 0 .../{isa => x86}/macro_assembler_x86_att.nim | 0 .../macro_assembler_x86_intel.nim | 0 .../platforms/{isa => x86}/simd_x86.nim | 0 tests/gpu/hello_world_llvm.nim | 2 +- tests/gpu/hello_world_nvidia.nim | 2 +- tests/gpu/t_nvidia_fp.nim | 22 +++++++++---------- 22 files changed, 30 insertions(+), 29 deletions(-) rename constantine/{platforms/code_generator/nvidia.nim => math_compiler/codegen_nvidia.nim} (98%) rename constantine/math_compiler/{fields_nvidia.nim => impl_fields_nvidia.nim} (99%) rename constantine/{platforms/code_generator => math_compiler}/ir.nim (98%) rename constantine/platforms/{code_generator => llvm}/README.md (90%) rename constantine/platforms/{code_generator => llvm}/bindings/c_abi.nim (100%) rename constantine/platforms/{code_generator => llvm}/bindings/llvm_abi.nim (100%) rename constantine/platforms/{code_generator => llvm}/bindings/nvidia_abi.nim (100%) rename constantine/platforms/{code_generator => llvm}/llvm.nim (100%) rename constantine/platforms/{code_generator => llvm}/nvidia_inlineasm.nim (100%) rename constantine/platforms/{isa => x86}/cpudetect_x86.nim (100%) rename constantine/platforms/{isa => x86}/macro_assembler_x86.nim (100%) rename constantine/platforms/{isa => x86}/macro_assembler_x86_att.nim (100%) rename constantine/platforms/{isa => x86}/macro_assembler_x86_intel.nim (100%) rename constantine/platforms/{isa => x86}/simd_x86.nim (100%) diff --git a/constantine/hashes/sha256/sha256_x86_shaext.nim b/constantine/hashes/sha256/sha256_x86_shaext.nim index 78a42ee6..0accdced 100644 --- a/constantine/hashes/sha256/sha256_x86_shaext.nim +++ b/constantine/hashes/sha256/sha256_x86_shaext.nim @@ -7,7 +7,7 @@ # at your option. This file may not be copied, modified, or distributed except according to those terms. import - constantine/platforms/isa/simd_x86, + constantine/platforms/x86/simd_x86, constantine/platforms/primitives, ./sha256_generic diff --git a/constantine/hashes/sha256/sha256_x86_ssse3.nim b/constantine/hashes/sha256/sha256_x86_ssse3.nim index 8e774f26..f716cd53 100644 --- a/constantine/hashes/sha256/sha256_x86_ssse3.nim +++ b/constantine/hashes/sha256/sha256_x86_ssse3.nim @@ -7,7 +7,7 @@ # at your option. This file may not be copied, modified, or distributed except according to those terms. import - constantine/platforms/isa/simd_x86, + constantine/platforms/x86/simd_x86, constantine/platforms/primitives, ./sha256_generic diff --git a/constantine/math/extension_fields/towers.nim b/constantine/math/extension_fields/towers.nim index 20fc0d51..456684e5 100644 --- a/constantine/math/extension_fields/towers.nim +++ b/constantine/math/extension_fields/towers.nim @@ -57,8 +57,8 @@ type CubicExt[Fp2[Name]] Fp12*[Name: static Algebra] = - CubicExt[Fp4[Name]] - # QuadraticExt[Fp6[Name]] + # CubicExt[Fp4[Name]] + QuadraticExt[Fp6[Name]] template c0*(a: ExtensionField): auto = a.coords[0] diff --git a/constantine/platforms/code_generator/nvidia.nim b/constantine/math_compiler/codegen_nvidia.nim similarity index 98% rename from constantine/platforms/code_generator/nvidia.nim rename to constantine/math_compiler/codegen_nvidia.nim index 82707b66..72df96b4 100644 --- a/constantine/platforms/code_generator/nvidia.nim +++ b/constantine/math_compiler/codegen_nvidia.nim @@ -7,11 +7,11 @@ # at your option. This file may not be copied, modified, or distributed except according to those terms. import - ./bindings/nvidia_abi {.all.}, - ./bindings/c_abi, - ./llvm, ./ir, - ./nvidia_inlineasm, - ../primitives + constantine/platforms/llvm/bindings/nvidia_abi {.all.}, + constantine/platforms/llvm/bindings/c_abi, + constantine/platforms/llvm/[llvm, nvidia_inlineasm], + constantine/platforms/primitives, + ./ir export nvidia_abi, nvidia_inlineasm, diff --git a/constantine/math_compiler/fields_nvidia.nim b/constantine/math_compiler/impl_fields_nvidia.nim similarity index 99% rename from constantine/math_compiler/fields_nvidia.nim rename to constantine/math_compiler/impl_fields_nvidia.nim index a440129a..0ffbb5b1 100644 --- a/constantine/math_compiler/fields_nvidia.nim +++ b/constantine/math_compiler/impl_fields_nvidia.nim @@ -7,7 +7,8 @@ # at your option. This file may not be copied, modified, or distributed except according to those terms. import - ../platforms/code_generator/[llvm, nvidia, ir] + ../platforms/llvm/llvm, + ./ir, ./codegen_nvidia # ############################################################ # diff --git a/constantine/platforms/code_generator/ir.nim b/constantine/math_compiler/ir.nim similarity index 98% rename from constantine/platforms/code_generator/ir.nim rename to constantine/math_compiler/ir.nim index 801d22cb..43aa8eaf 100644 --- a/constantine/platforms/code_generator/ir.nim +++ b/constantine/math_compiler/ir.nim @@ -9,9 +9,9 @@ import constantine/math/config/[curves, precompute], constantine/math/io/io_bigints, - ../primitives, ../bithacks, - ../../serialization/[endians, codecs, io_limbs], - ./llvm + constantine/platforms/[primitives, bithacks], + constantine/platforms/llvm/llvm, + constantine/serialization/[endians, codecs, io_limbs] # ############################################################ # diff --git a/constantine/platforms/code_generator/README.md b/constantine/platforms/llvm/README.md similarity index 90% rename from constantine/platforms/code_generator/README.md rename to constantine/platforms/llvm/README.md index 4c2a5426..7441e9c8 100644 --- a/constantine/platforms/code_generator/README.md +++ b/constantine/platforms/llvm/README.md @@ -1,6 +1,6 @@ # GPU compilation targets -For CPUs, Constantine JIT-compiles the cryptographic kernels via LLVM. +For GPUs, Constantine JIT-compiles the cryptographic kernels via LLVM. This allows targeting several devices with a single frontend, the LLVM IR. diff --git a/constantine/platforms/code_generator/bindings/c_abi.nim b/constantine/platforms/llvm/bindings/c_abi.nim similarity index 100% rename from constantine/platforms/code_generator/bindings/c_abi.nim rename to constantine/platforms/llvm/bindings/c_abi.nim diff --git a/constantine/platforms/code_generator/bindings/llvm_abi.nim b/constantine/platforms/llvm/bindings/llvm_abi.nim similarity index 100% rename from constantine/platforms/code_generator/bindings/llvm_abi.nim rename to constantine/platforms/llvm/bindings/llvm_abi.nim diff --git a/constantine/platforms/code_generator/bindings/nvidia_abi.nim b/constantine/platforms/llvm/bindings/nvidia_abi.nim similarity index 100% rename from constantine/platforms/code_generator/bindings/nvidia_abi.nim rename to constantine/platforms/llvm/bindings/nvidia_abi.nim diff --git a/constantine/platforms/code_generator/llvm.nim b/constantine/platforms/llvm/llvm.nim similarity index 100% rename from constantine/platforms/code_generator/llvm.nim rename to constantine/platforms/llvm/llvm.nim diff --git a/constantine/platforms/code_generator/nvidia_inlineasm.nim b/constantine/platforms/llvm/nvidia_inlineasm.nim similarity index 100% rename from constantine/platforms/code_generator/nvidia_inlineasm.nim rename to constantine/platforms/llvm/nvidia_inlineasm.nim diff --git a/constantine/platforms/metering/benchmarking.nim b/constantine/platforms/metering/benchmarking.nim index d89b57d9..e9385bf8 100644 --- a/constantine/platforms/metering/benchmarking.nim +++ b/constantine/platforms/metering/benchmarking.nim @@ -7,7 +7,7 @@ # at your option. This file may not be copied, modified, or distributed except according to those terms. when defined(amd64): # TODO defined(i386) but it seems like RDTSC call is misconfigured - from ../isa/cpudetect_x86 import cpuName_x86 + from ../x86/cpudetect_x86 import cpuName_x86 const SupportsCPUName* = true const SupportsGetTicks* = true diff --git a/constantine/platforms/primitives.nim b/constantine/platforms/primitives.nim index 413a560e..72a9ba70 100644 --- a/constantine/platforms/primitives.nim +++ b/constantine/platforms/primitives.nim @@ -39,7 +39,7 @@ export # This will impact benchmarks that do not need it, such as the threadpool. when X86 and GCC_Compatible: - import isa/[cpudetect_x86, macro_assembler_x86] + import x86/[cpudetect_x86, macro_assembler_x86] export cpudetect_x86, macro_assembler_x86 # No exceptions allowed in core cryptographic operations diff --git a/constantine/platforms/isa/cpudetect_x86.nim b/constantine/platforms/x86/cpudetect_x86.nim similarity index 100% rename from constantine/platforms/isa/cpudetect_x86.nim rename to constantine/platforms/x86/cpudetect_x86.nim diff --git a/constantine/platforms/isa/macro_assembler_x86.nim b/constantine/platforms/x86/macro_assembler_x86.nim similarity index 100% rename from constantine/platforms/isa/macro_assembler_x86.nim rename to constantine/platforms/x86/macro_assembler_x86.nim diff --git a/constantine/platforms/isa/macro_assembler_x86_att.nim b/constantine/platforms/x86/macro_assembler_x86_att.nim similarity index 100% rename from constantine/platforms/isa/macro_assembler_x86_att.nim rename to constantine/platforms/x86/macro_assembler_x86_att.nim diff --git a/constantine/platforms/isa/macro_assembler_x86_intel.nim b/constantine/platforms/x86/macro_assembler_x86_intel.nim similarity index 100% rename from constantine/platforms/isa/macro_assembler_x86_intel.nim rename to constantine/platforms/x86/macro_assembler_x86_intel.nim diff --git a/constantine/platforms/isa/simd_x86.nim b/constantine/platforms/x86/simd_x86.nim similarity index 100% rename from constantine/platforms/isa/simd_x86.nim rename to constantine/platforms/x86/simd_x86.nim diff --git a/tests/gpu/hello_world_llvm.nim b/tests/gpu/hello_world_llvm.nim index d79f133c..7fcec43f 100644 --- a/tests/gpu/hello_world_llvm.nim +++ b/tests/gpu/hello_world_llvm.nim @@ -6,7 +6,7 @@ # * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0). # at your option. This file may not be copied, modified, or distributed except according to those terms. -import constantine/platforms/code_generator/llvm +import constantine/platforms/llvm/llvm echo "LLVM JIT compiler Hello World" diff --git a/tests/gpu/hello_world_nvidia.nim b/tests/gpu/hello_world_nvidia.nim index 1eaa4fee..1f81ce54 100644 --- a/tests/gpu/hello_world_nvidia.nim +++ b/tests/gpu/hello_world_nvidia.nim @@ -6,7 +6,7 @@ # * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0). # at your option. This file may not be copied, modified, or distributed except according to those terms. -import constantine/platforms/code_generator/[llvm, nvidia, bindings/c_abi] +import constantine/platforms/llvm/[llvm, nvidia, bindings/c_abi] # ############################################################ # diff --git a/tests/gpu/t_nvidia_fp.nim b/tests/gpu/t_nvidia_fp.nim index e43b6512..e40a3c0e 100644 --- a/tests/gpu/t_nvidia_fp.nim +++ b/tests/gpu/t_nvidia_fp.nim @@ -11,12 +11,12 @@ import # Standard library std/[unittest, times], # Internal - constantine/platforms/code_generator/[llvm, nvidia, ir], + constantine/platforms/llvm/llvm, constantine/platforms/static_for, constantine/named/algebras, constantine/math/io/io_bigints, constantine/math/arithmetic, - constantine/math_compiler/fields_nvidia, + constantine/math_compiler/[ir, impl_fields_nvidia, codegen_nvidia], # Test utilities helpers/prng_unsafe @@ -28,14 +28,14 @@ echo "test_nvidia_fp xoshiro512** seed: ", seed const Iters = 10 -proc init(T: type CurveMetadata, asy: Assembler_LLVM, name: static Algebra, wordSize: WordSize): T = +proc init(T: type CurveMetadata, asy: Assembler_LLVM, curve: static Algebra, wordSize: WordSize): T = CurveMetadata.init( asy.ctx, - $name & "_", wordSize, - fpBits = uint32 Fp[name].bits(), - fpMod = Fp[name].getModulus().toHex(), - frBits = uint32 Fr[name].bits(), - frMod = Fr[name].getModulus().toHex()) + $curve & "_", wordSize, + fpBits = uint32 Fp[curve].bits(), + fpMod = Fp[curve].getModulus().toHex(), + frBits = uint32 Fr[curve].bits(), + frMod = Fr[curve].getModulus().toHex()) proc genFieldAddPTX(asy: Assembler_LLVM, cm: CurveMetadata) = let fpAdd = asy.field_add_gen(cm, fp) @@ -67,7 +67,7 @@ var sm: tuple[major, minor: int32] check cuDeviceGetAttribute(sm.major, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR, cudaDevice) check cuDeviceGetAttribute(sm.minor, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR, cudaDevice) -proc t_field_add(name: static Algebra) = +proc t_field_add(curve: static Algebra) = # Codegen # ------------------------- let asy = Assembler_LLVM.new(bkNvidiaPTX, cstring("t_nvidia_" & $curve)) @@ -121,7 +121,7 @@ proc t_field_add(name: static Algebra) = doAssert bool(rCPU == rGPU_32) doAssert bool(rCPU == rGPU_64) -proc t_field_sub(name: static Algebra) = +proc t_field_sub(curve: static Algebra) = # Codegen # ------------------------- let asy = Assembler_LLVM.new(bkNvidiaPTX, cstring("t_nvidia_" & $curve)) @@ -175,7 +175,7 @@ proc t_field_sub(name: static Algebra) = doAssert bool(rCPU == rGPU_32) doAssert bool(rCPU == rGPU_64) -proc t_field_mul(name: static Algebra) = +proc t_field_mul(curve: static Algebra) = # Codegen # ------------------------- let asy = Assembler_LLVM.new(bkNvidiaPTX, cstring("t_nvidia_" & $curve)) From 7597823a9eeab291a4320f2f6f9888da9079ce1a Mon Sep 17 00:00:00 2001 From: Mamy Ratsimbazafy Date: Sun, 21 Jul 2024 01:32:09 +0200 Subject: [PATCH 2/2] revert towering for Fp12 --- constantine/math/extension_fields/towers.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/constantine/math/extension_fields/towers.nim b/constantine/math/extension_fields/towers.nim index 456684e5..20fc0d51 100644 --- a/constantine/math/extension_fields/towers.nim +++ b/constantine/math/extension_fields/towers.nim @@ -57,8 +57,8 @@ type CubicExt[Fp2[Name]] Fp12*[Name: static Algebra] = - # CubicExt[Fp4[Name]] - QuadraticExt[Fp6[Name]] + CubicExt[Fp4[Name]] + # QuadraticExt[Fp6[Name]] template c0*(a: ExtensionField): auto = a.coords[0]