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

Fresh build fail to compile sample #975

Closed
mingodad opened this issue Sep 5, 2023 · 54 comments
Closed

Fresh build fail to compile sample #975

mingodad opened this issue Sep 5, 2023 · 54 comments

Comments

@mingodad
Copy link

mingodad commented Sep 5, 2023

After a fresh build of c3c and testing the sample shown bellow I'm getting several errors:

//./c3c compile exprtk_functional_test.c3 -l pthread
import std::io;
import std::math;

int total_count = 0;
int total_eq = 0;

fn double myfabs(double n) {return math::abs(n);}

fn void equal(double a, double b) {
	++total_count;
	if(a == b) {
		++total_eq;
	}
	else {
		io::printf("%17f\t%17f\n", a, b);
	}
}

fn int main()
{
	double x, y, z, w;

	x=11.12345678910737373;
	y=22.12345678910737373;
	z=33.12345678910737373;
	w=44.12345678910737373;


equal((1.003727171046591100000000000000),((((x+y)/z))));
equal((1101.252705279183400000000000000000),((((x+y)*z))));
equal((0.123456789107372340000000000000),((((x+y)-z))));
equal(2.2*1.1/2.2,1.1);
equal(2.2*(1.1/2.2),1.1);


io::printf("%d\t%d\n", total_count, total_eq);

return 0;
}

Output of invoking compilation:

./c3c compile sample.c3 -l pthread
 70: 		self.has_last = false;
 71: 		return IoError.EOF?;
 72: 	}
 73: 	usz n = min(readable, bytes.len);
     	        ^^^
(c3c/lib/std/io/stream/bytebuffer.c3:73:10) Error: Macros from other modules must be prefixed with the module name.

16: fn usz! ByteReader.read(&self, char[] bytes)
17: {
18: 	if (self.index >= self.bytes.len) return IoError.EOF?;
19: 	usz len = min(self.bytes.len - self.index, bytes.len);
    	          ^^^
(c3c/lib/std/io/stream/bytereader.c3:19:12) Error: The macro 'min' is defined in both 'std::math' and 'std::time', please use either std::math::min or std::time::min to resolve the ambiguity.

 43: 		}
 44: 		self.refill()!;
 45: 	}
 46: 	usz n = min(self.write_idx - self.read_idx, bytes.len);
     	        ^^^
(c3c/lib/std/io/stream/buffer.c3:46:10) Error: The macro 'min' is defined in both 'std::math' and 'std::time', please use either std::math::min or std::time::min to resolve the ambiguity.

20: fn usz! LimitReader.read(&self, char[] bytes)
21: {
22: 	if (self.limit == 0) return IoError.EOF?;
23: 	usz m = min(bytes.len, self.limit);
    	        ^^^
(c3c/lib/std/io/stream/limitreader.c3:23:10) Error: The macro 'min' is defined in both 'std::math' and 'std::time', please use either std::math::min or std::time::min to resolve the ambiguity.

 55: 	while ((result = posix::pthread_mutex_trylock(mtx)) == errno::EBUSY)
 56: 	{
 57: 		if (!ms) break;
 58: 		ulong sleep = min(5, ms);
     		              ^^^
(c3c/lib/std/threads/os/thread_posix.c3:58:17) Error: The macro 'min' is defined in both 'std::math' and 'std::time', please use either std::math::min or std::time::min to resolve the ambiguity.
@mingodad
Copy link
Author

mingodad commented Sep 5, 2023

Can an option like -fsyntax-only be added to c3c ?
With that we can try check all .c3 files to catch errors like the ones pointed here.

@lerno
Copy link
Collaborator

lerno commented Sep 5, 2023

You can use -C to only check the code.

@lerno
Copy link
Collaborator

lerno commented Sep 5, 2023

I cannot reproduce this with the given input.

@mingodad
Copy link
Author

mingodad commented Sep 5, 2023

I'm testing it with this now:

for fn in `find ../../c3c -name '*.c3' | sort`
do
     echo $fn
     ./c3c -C compile $fn
done

And I'm getting this:

../../c3c/build/lib/std/ascii.c3
1: module std::ascii;
2: 
3: macro bool in_range_m(c, start, len) => (uint)(c - start) < len;
              ^^^^^^^^^^
(c3c/lib/std/ascii.c3:3:12) Error: 'in_range_m' would shadow a previous declaration.

1: module std::ascii;
2: 
3: macro bool in_range_m(c, start, len) => (uint)(c - start) < len;
              ^^^^^^^^^^
(c3c/build/lib/std/ascii.c3:3:12) Note: The previous use of 'in_range_m' was here.

 1: module std::ascii;
 2: 
 3: macro bool in_range_m(c, start, len) => (uint)(c - start) < len;
 4: macro bool is_lower_m(c) => in_range_m(c, 0x61, 26);
               ^^^^^^^^^^
(c3c/lib/std/ascii.c3:4:12) Error: 'is_lower_m' would shadow a previous declaration.
...
../../c3c/build/lib/std/math/math_nolibc/sin.c3
 70: 		self.has_last = false;
 71: 		return IoError.EOF?;
 72: 	}
 73: 	usz n = min(readable, bytes.len);
     	        ^^^
(c3c/lib/std/io/stream/bytebuffer.c3:73:10) Error: Macros from other modules must be prefixed with the module name.

16: fn usz! ByteReader.read(&self, char[] bytes)
17: {
18: 	if (self.index >= self.bytes.len) return IoError.EOF?;
19: 	usz len = min(self.bytes.len - self.index, bytes.len);
    	          ^^^
(c3c/lib/std/io/stream/bytereader.c3:19:12) Error: The macro 'min' is defined in both 'std::math' and 'std::time', please use either std::math::min or std::time::min to resolve the ambiguity.

 43: 		}
 44: 		self.refill()!;
 45: 	}
 46: 	usz n = min(self.write_idx - self.read_idx, bytes.len);
     	        ^^^
(c3c/lib/std/io/stream/buffer.c3:46:10) Error: The macro 'min' is defined in both 'std::math' and 'std::time', please use either std::math::min or std::time::min to resolve the ambiguity.

@mingodad
Copy link
Author

mingodad commented Sep 5, 2023

My fresh c3c:

./c3c -V
C3 Compiler Version (pre-alpha):   0.4.632
Installed directory:               /home/mingo/dev/c/A_programming-languages/c3c/build/
LLVM version:                      15.0.7
LLVM default target:               x86_64-unknown-linux-gnu

@mingodad
Copy link
Author

mingodad commented Sep 5, 2023

Also when I do a make install only the binary is installed no libraries are installed:

clang-15-env mycmake -DCMAKE_INSTALL_PREFIX=$HOME/local/c3c -DCMAKE_BUILD_TYPE=Release ..
make
...
make install
Consolidate compiler generated dependencies of target c3c_wrappers
[  2%] Built target c3c_wrappers
Consolidate compiler generated dependencies of target miniz
[  4%] Built target miniz
Consolidate compiler generated dependencies of target c3c
[100%] Built target c3c
Install the project...
-- Install configuration: "Release"
-- Installing: /home/mingo/local/c3c/bin/c3c
ls -R /home/mingo/local/c3c
/home/mingo/local/c3c:
bin

/home/mingo/local/c3c/bin:
c3c

@lerno
Copy link
Collaborator

lerno commented Sep 5, 2023

If you are compiling a standard library file, then things will end up going bad since the standard library is by default added to the files included. So do exclude anything in /lib/std

@mingodad
Copy link
Author

mingodad commented Sep 5, 2023

Let's start again from scratch (that worked some time ago):

git clone https://github.com/c3lang/c3c.git c3c0
cd c3c0
mkdir build
cd build
clang-15-env mycmake -DCMAKE_INSTALL_PREFIX=$HOME/local/c3c -DCMAKE_BUILD_TYPE=Release ..
-- The C compiler identification is Clang 15.0.7
-- The CXX compiler identification is Clang 15.0.7
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /home/mingo/local/clang-15/bin/clang - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /home/mingo/local/clang-15/bin/clang++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
C3C version: 0.4.633
-- Found CURL: /usr/lib/x86_64-linux-gnu/libcurl.so (found version "7.58.0")  
-- Performing Test Terminfo_LINKABLE
-- Performing Test Terminfo_LINKABLE - Success
-- Found Terminfo: /usr/lib/x86_64-linux-gnu/libtinfo.so  
-- Found ZLIB: /usr/lib/x86_64-linux-gnu/libz.so (found version "1.2.11") 
-- Found zstd: /usr/lib/x86_64-linux-gnu/libzstd.so  
-- Found LibXml2: /usr/lib/x86_64-linux-gnu/libxml2.so (found version "2.9.4") 
-- Found LLVM 15.0.3
-- Using LLVMConfig.cmake in: /home/mingo/local/clang-15/lib/cmake/llvm
-- Libraries located in: /home/mingo/local/clang-15/lib
-- LLVM was not built with RTTI
-- using find_library
-- linking to llvm libs /home/mingo/local/clang-15/lib/liblldCOFF.a;/home/mingo/local/clang-15/lib/liblldCommon.a;/home/mingo/local/clang-15/lib/liblldWasm.a;/home/mingo/local/clang-15/lib/liblldMinGW.a;/home/mingo/local/clang-15/lib/liblldELF.a;/home/mingo/local/clang-15/lib/liblldMachO.a
-- Found lld libs /home/mingo/local/clang-15/lib/liblldCOFF.a;/home/mingo/local/clang-15/lib/liblldCommon.a;/home/mingo/local/clang-15/lib/liblldWasm.a;/home/mingo/local/clang-15/lib/liblldMinGW.a;/home/mingo/local/clang-15/lib/liblldELF.a;/home/mingo/local/clang-15/lib/liblldMachO.a
-- using gcc/clang warning switches
-- The following OPTIONAL packages have been found:

 * CURL
 * Terminfo
 * ZLIB
 * zstd
 * LibXml2

-- The following REQUIRED packages have been found:

 * LLVM

-- Configuring done
-- Generating done
-- Build files have been written to: /home/mingo/dev/c/A_programming-languages/c3c0/build

mingo@mingo-X550VX:~/dev/c/A_programming-languages/c3c0/build$ make
[  1%] Building CXX object CMakeFiles/c3c_wrappers.dir/wrapper/src/wrapper.cpp.o
[  2%] Linking CXX static library libc3c_wrappers.a
[  2%] Built target c3c_wrappers
[  3%] Building C object CMakeFiles/miniz.dir/dependencies/miniz/miniz.c.o
[  4%] Linking C static library libminiz.a
[  4%] Built target miniz
[  5%] Building C object CMakeFiles/c3c.dir/src/build/builder.c.o
[  6%] Building C object CMakeFiles/c3c.dir/src/build/build_options.c.o
[  8%] Building C object CMakeFiles/c3c.dir/src/build/project_creation.c.o
[  9%] Building C object CMakeFiles/c3c.dir/src/compiler/ast.c.o
[ 10%] Building C object CMakeFiles/c3c.dir/src/compiler/bigint.c.o
[ 11%] Building C object CMakeFiles/c3c.dir/src/compiler/codegen_general.c.o
[ 12%] Building C object CMakeFiles/c3c.dir/src/compiler/compiler.c.o
[ 13%] Building C object CMakeFiles/c3c.dir/src/compiler/context.c.o
[ 15%] Building C object CMakeFiles/c3c.dir/src/compiler/copying.c.o
[ 16%] Building C object CMakeFiles/c3c.dir/src/compiler/diagnostics.c.o
[ 17%] Building C object CMakeFiles/c3c.dir/src/compiler/float.c.o
[ 18%] Building C object CMakeFiles/c3c.dir/src/compiler/headers.c.o
[ 19%] Building C object CMakeFiles/c3c.dir/src/compiler/json_output.c.o
[ 20%] Building C object CMakeFiles/c3c.dir/src/compiler/lexer.c.o
[ 22%] Building C object CMakeFiles/c3c.dir/src/compiler/libraries.c.o
[ 23%] Building C object CMakeFiles/c3c.dir/src/compiler/linker.c.o
[ 24%] Building C object CMakeFiles/c3c.dir/src/compiler/llvm_codegen.c.o
[ 25%] Building C object CMakeFiles/c3c.dir/src/compiler/abi/c_abi_aarch64.c.o
[ 26%] Building C object CMakeFiles/c3c.dir/src/compiler/abi/c_abi.c.o
[ 27%] Building C object CMakeFiles/c3c.dir/src/compiler/abi/c_abi_riscv.c.o
[ 29%] Building C object CMakeFiles/c3c.dir/src/compiler/abi/c_abi_wasm.c.o
[ 30%] Building C object CMakeFiles/c3c.dir/src/compiler/abi/c_abi_win64.c.o
[ 31%] Building C object CMakeFiles/c3c.dir/src/compiler/abi/c_abi_x64.c.o
[ 32%] Building C object CMakeFiles/c3c.dir/src/compiler/abi/c_abi_x86.c.o
[ 33%] Building C object CMakeFiles/c3c.dir/src/compiler/llvm_codegen_debug_info.c.o
[ 34%] Building C object CMakeFiles/c3c.dir/src/compiler/llvm_codegen_expr.c.o
[ 36%] Building C object CMakeFiles/c3c.dir/src/compiler/llvm_codegen_function.c.o
[ 37%] Building C object CMakeFiles/c3c.dir/src/compiler/llvm_codegen_instr.c.o
[ 38%] Building C object CMakeFiles/c3c.dir/src/compiler/llvm_codegen_module.c.o
[ 39%] Building C object CMakeFiles/c3c.dir/src/compiler/llvm_codegen_stmt.c.o
[ 40%] Building C object CMakeFiles/c3c.dir/src/compiler/llvm_codegen_type.c.o
[ 41%] Building C object CMakeFiles/c3c.dir/src/compiler/llvm_codegen_value.c.o
[ 43%] Building C object CMakeFiles/c3c.dir/src/compiler/module.c.o
[ 44%] Building C object CMakeFiles/c3c.dir/src/compiler/number.c.o
[ 45%] Building C object CMakeFiles/c3c.dir/src/compiler/parse_expr.c.o
[ 46%] Building C object CMakeFiles/c3c.dir/src/compiler/parse_global.c.o
[ 47%] Building C object CMakeFiles/c3c.dir/src/compiler/parser.c.o
[ 48%] Building C object CMakeFiles/c3c.dir/src/compiler/parse_stmt.c.o
[ 50%] Building C object CMakeFiles/c3c.dir/src/compiler/sema_casts.c.o
[ 51%] Building C object CMakeFiles/c3c.dir/src/compiler/sema_decls.c.o
[ 52%] Building C object CMakeFiles/c3c.dir/src/compiler/sema_expr.c.o
[ 53%] Building C object CMakeFiles/c3c.dir/src/compiler/sema_name_resolution.c.o
[ 54%] Building C object CMakeFiles/c3c.dir/src/compiler/sema_errors.c.o
[ 55%] Building C object CMakeFiles/c3c.dir/src/compiler/sema_builtins.c.o
[ 56%] Building C object CMakeFiles/c3c.dir/src/compiler/sema_initializers.c.o
[ 58%] Building C object CMakeFiles/c3c.dir/src/compiler/semantic_analyser.c.o
[ 59%] Building C object CMakeFiles/c3c.dir/src/compiler/sema_passes.c.o
[ 60%] Building C object CMakeFiles/c3c.dir/src/compiler/sema_stmts.c.o
[ 61%] Building C object CMakeFiles/c3c.dir/src/compiler/sema_types.c.o
[ 62%] Building C object CMakeFiles/c3c.dir/src/compiler/source_file.c.o
[ 63%] Building C object CMakeFiles/c3c.dir/src/compiler/symtab.c.o
[ 65%] Building C object CMakeFiles/c3c.dir/src/compiler/target.c.o
[ 66%] Building C object CMakeFiles/c3c.dir/src/compiler/sema_asm.c.o
[ 67%] Building C object CMakeFiles/c3c.dir/src/compiler_tests/benchmark.c.o
[ 68%] Building C object CMakeFiles/c3c.dir/src/compiler_tests/tests.c.o
[ 69%] Building C object CMakeFiles/c3c.dir/src/compiler/tokens.c.o
[ 70%] Building C object CMakeFiles/c3c.dir/src/compiler/types.c.o
[ 72%] Building C object CMakeFiles/c3c.dir/src/main.c.o
[ 73%] Building C object CMakeFiles/c3c.dir/src/utils/errors.c.o
[ 74%] Building C object CMakeFiles/c3c.dir/src/utils/file_utils.c.o
[ 75%] Building C object CMakeFiles/c3c.dir/src/utils/find_msvc.c.o
[ 76%] Building C object CMakeFiles/c3c.dir/src/utils/malloc.c.o
[ 77%] Building C object CMakeFiles/c3c.dir/src/utils/stringutils.c.o
[ 79%] Building C object CMakeFiles/c3c.dir/src/utils/taskqueue.c.o
[ 80%] Building C object CMakeFiles/c3c.dir/src/utils/json.c.o
[ 81%] Building C object CMakeFiles/c3c.dir/src/build/project.c.o
[ 82%] Building C object CMakeFiles/c3c.dir/src/utils/vmem.c.o
[ 83%] Building C object CMakeFiles/c3c.dir/src/utils/whereami.c.o
[ 84%] Building C object CMakeFiles/c3c.dir/src/utils/cpus.c.o
[ 86%] Building C object CMakeFiles/c3c.dir/src/utils/unzipper.c.o
[ 87%] Building C object CMakeFiles/c3c.dir/src/compiler/decltable.c.o
[ 88%] Building C object CMakeFiles/c3c.dir/src/compiler/mac_support.c.o
[ 89%] Building C object CMakeFiles/c3c.dir/src/compiler/llvm_codegen_storeload.c.o
[ 90%] Building C object CMakeFiles/c3c.dir/src/compiler/windows_support.c.o
[ 91%] Building C object CMakeFiles/c3c.dir/src/compiler/codegen_asm.c.o
[ 93%] Building C object CMakeFiles/c3c.dir/src/compiler/asm_target.c.o
[ 94%] Building C object CMakeFiles/c3c.dir/src/compiler/llvm_codegen_builtins.c.o
[ 95%] Building C object CMakeFiles/c3c.dir/src/compiler/expr.c.o
[ 96%] Building C object CMakeFiles/c3c.dir/src/utils/time.c.o
[ 97%] Building C object CMakeFiles/c3c.dir/src/utils/http.c.o
[ 98%] Building C object CMakeFiles/c3c.dir/src/compiler/sema_liveness.c.o
[100%] Linking CXX executable c3c
[100%] Built target c3c

mingo@mingo-X550VX:~/dev/c/A_programming-languages/c3c0/build$ ./c3c -V
C3 Compiler Version (pre-alpha):   0.4.633
Installed directory:               /home/mingo/dev/c/A_programming-languages/c3c0/build/
LLVM version:                      15.0.7
LLVM default target:               x86_64-unknown-linux-gnu

mingo@mingo-X550VX:~/dev/c/A_programming-languages/c3c0/build$ ./c3c compile exprtk_functional_test.c3 
 70: 		self.has_last = false;
 71: 		return IoError.EOF?;
 72: 	}
 73: 	usz n = min(readable, bytes.len);
     	        ^^^
(/home/mingo/dev/c/A_programming-languages/c3c0/lib/std/io/stream/bytebuffer.c3:73:10) Error: Macros from other modules must be prefixed with the module name.

16: fn usz! ByteReader.read(&self, char[] bytes)
17: {
18: 	if (self.index >= self.bytes.len) return IoError.EOF?;
19: 	usz len = min(self.bytes.len - self.index, bytes.len);
    	          ^^^
(/home/mingo/dev/c/A_programming-languages/c3c0/lib/std/io/stream/bytereader.c3:19:12) Error: The macro needs a path prefix (e.g. 'std::math::min').

 43: 		}
 44: 		self.refill()!;
 45: 	}
 46: 	usz n = min(self.write_idx - self.read_idx, bytes.len);
     	        ^^^
(/home/mingo/dev/c/A_programming-languages/c3c0/lib/std/io/stream/buffer.c3:46:10) Error: The macro needs a path prefix (e.g. 'std::math::min').

20: fn usz! LimitReader.read(&self, char[] bytes)
21: {
22: 	if (self.limit == 0) return IoError.EOF?;
23: 	usz m = min(bytes.len, self.limit);
    	        ^^^
(/home/mingo/dev/c/A_programming-languages/c3c0/lib/std/io/stream/limitreader.c3:23:10) Error: The macro needs a path prefix (e.g. 'std::math::min').

 55: 	while ((result = posix::pthread_mutex_trylock(mtx)) == errno::EBUSY)
 56: 	{
 57: 		if (!ms) break;
 58: 		ulong sleep = min(5, ms);
     		              ^^^
(/home/mingo/dev/c/A_programming-languages/c3c0/lib/std/threads/os/thread_posix.c3:58:17) Error: The macro needs a path prefix (e.g. 'std::math::min').

@lerno
Copy link
Collaborator

lerno commented Sep 5, 2023

Looking at this, the only explanation I have would be that you're missing std/core/builtin_comparison.c3. Can you list all the files in /home/mingo/dev/c/A_programming-languages/c3c0/lib/std/core

@lerno
Copy link
Collaborator

lerno commented Sep 5, 2023

If it's there, do you find the macro min(x, ...) @builtin declaration?

@lerno
Copy link
Collaborator

lerno commented Sep 5, 2023

install missing the library is indeed an omission.

@lerno
Copy link
Collaborator

lerno commented Sep 5, 2023

The files you should be able to test are the ones in resources aside from the ones in "notworking"

@mingodad
Copy link
Author

mingodad commented Sep 5, 2023

ls -l -R lib/std/core
lib/std/core:
total 148
drwxrwxr-x 2 mingo mingo  4096 sep  5 18:12 allocators
-rw-rw-r-- 1 mingo mingo  1966 sep  5 18:12 array.c3
-rw-rw-r-- 1 mingo mingo  3398 sep  5 18:12 bitorder.c3
-rw-rw-r-- 1 mingo mingo  8950 sep  5 18:12 builtin.c3
-rw-rw-r-- 1 mingo mingo  2549 sep  5 18:12 builtin_comparison.c3
-rw-rw-r-- 1 mingo mingo  1852 sep  5 18:12 cinterop.c3
-rw-rw-r-- 1 mingo mingo 11043 sep  5 18:12 conv.c3
-rw-rw-r-- 1 mingo mingo  9255 sep  5 18:12 dstring.c3
-rw-rw-r-- 1 mingo mingo  5097 sep  5 18:12 env.c3
-rw-rw-r-- 1 mingo mingo  3018 sep  5 18:12 mem_allocator.c3
-rw-rw-r-- 1 mingo mingo 17174 sep  5 18:12 mem.c3
drwxrwxr-x 2 mingo mingo  4096 sep  5 18:12 os
drwxrwxr-x 2 mingo mingo  4096 sep  5 18:12 private
-rw-rw-r-- 1 mingo mingo  5792 sep  5 18:12 runtime.c3
-rw-rw-r-- 1 mingo mingo 12677 sep  5 18:12 string.c3
-rw-rw-r-- 1 mingo mingo   463 sep  5 18:12 string_iterator.c3
-rw-rw-r-- 1 mingo mingo 11025 sep  5 18:12 string_to_real.c3
-rw-rw-r-- 1 mingo mingo  7189 sep  5 18:12 types.c3
-rw-rw-r-- 1 mingo mingo   993 sep  5 18:12 values.c3

lib/std/core/allocators:
total 48
-rw-rw-r-- 1 mingo mingo 4658 sep  5 18:12 arena_allocator.c3
-rw-rw-r-- 1 mingo mingo 6936 sep  5 18:12 dynamic_arena.c3
-rw-rw-r-- 1 mingo mingo 5341 sep  5 18:12 heap_allocator.c3
-rw-rw-r-- 1 mingo mingo 3900 sep  5 18:12 mem_allocator_fn.c3
-rw-rw-r-- 1 mingo mingo 6056 sep  5 18:12 on_stack_allocator.c3
-rw-rw-r-- 1 mingo mingo 7204 sep  5 18:12 temp_allocator.c3
-rw-rw-r-- 1 mingo mingo 2647 sep  5 18:12 tracking_allocator.c3

lib/std/core/os:
total 4
-rw-rw-r-- 1 mingo mingo 744 sep  5 18:12 wasm_memory.c3

lib/std/core/private:
total 4
-rw-rw-r-- 1 mingo mingo 3849 sep  5 18:12 main_stub.c3

And yes it's there and has:

macro min(x, ...) @builtin
{
	$if $vacount == 1:
		return less(x, $vaarg(0)) ? x : $vaarg(0);
	$else
		var result = x;
		$for (var $i = 0; $i < $vacount; $i++)
			if (less($vaarg($i), result))
			{
				 result = $vaarg($i);
			}
		$endfor
		return result;
	$endif
}

@mingodad
Copy link
Author

mingodad commented Sep 5, 2023

After doing this changes:

------------------------- lib/std/io/stream/buffer.c3 -------------------------
index f47c8df2..e9855130 100644
@@ -43,7 +43,7 @@ fn usz! ReadBuffer.read(&self, char[] bytes)
 		}
 		self.refill()!;
 	}
-	usz n = min(self.write_idx - self.read_idx, bytes.len);
+	usz n = std::math::min(self.write_idx - self.read_idx, bytes.len);
 	bytes[:n] = self.bytes[self.read_idx:n];
 	self.read_idx += n;
 	return n;

----------------------- lib/std/io/stream/bytebuffer.c3 -----------------------
index ab0614fd..ce566a23 100644
@@ -70,7 +70,7 @@ fn usz! ByteBuffer.read(&self, char[] bytes)
 		self.has_last = false;
 		return IoError.EOF?;
 	}
-	usz n = min(readable, bytes.len);
+	usz n = std::math::min(readable, bytes.len);
 	bytes[:n] = self.bytes[self.read_idx:n];
 	self.read_idx += n;
 	self.has_last = n > 0;

----------------------- lib/std/io/stream/bytereader.c3 -----------------------
index c3197916..65eec308 100644
@@ -16,7 +16,7 @@ fn ByteReader* ByteReader.init(&self, char[] bytes)
 fn usz! ByteReader.read(&self, char[] bytes)
 {
 	if (self.index >= self.bytes.len) return IoError.EOF?;
-	usz len = min(self.bytes.len - self.index, bytes.len);
+	usz len = std::math::min(self.bytes.len - self.index, bytes.len);
 	if (len == 0) return 0;
 	mem::copy(bytes.ptr, &self.bytes[self.index], len);
 	self.index += len;

----------------------- lib/std/io/stream/limitreader.c3 -----------------------
index 87ba463d..14c8636c 100644
@@ -20,7 +20,7 @@ fn LimitReader* LimitReader.init(&self, Stream* wrapped_stream, usz limit)
 fn usz! LimitReader.read(&self, char[] bytes)
 {
 	if (self.limit == 0) return IoError.EOF?;
-	usz m = min(bytes.len, self.limit);
+	usz m = std::math::min(bytes.len, self.limit);
 	usz n = self.wrapped_stream.read(bytes[:m])!;
 	self.limit -= n;
 	return n;

---------------------- lib/std/threads/os/thread_posix.c3 ----------------------
index 5570d9a7..799fbde6 100644
@@ -55,7 +55,7 @@ fn void! NativeMutex.lock_timeout(&mtx, ulong ms)
 	while ((result = posix::pthread_mutex_trylock(mtx)) == errno::EBUSY)
 	{
 		if (!ms) break;
-		ulong sleep = min(5, ms);
+		ulong sleep = std::math::min(5, ms);
 		if (!libc::nanosleep(&& TimeSpec { .s = 0, .ns = (CLong)sleep * 1000_000 }, null)) return ThreadFault.LOCK_FAILED?;
 		ms -= sleep;
 	}

Again:

./c3c compile exprtk_functional_test.c3 
 55: 	while ((result = posix::pthread_mutex_trylock(mtx)) == errno::EBUSY)
 56: 	{
 57: 		if (!ms) break;
 58: 		ulong sleep = std::math::min(5, ms);
     		                         ^^^^^^^^^^
(/home/mingo/dev/c/A_programming-languages/c3c0/lib/std/threads/os/thread_posix.c3:58:28) Error: @require "types::is_same($typeof(x), $typeof(y))" violated: 'The input types must be equal'.

Then changing ulong sleep = std::math::min(5, ms); by ulong sleep = std::math::min(5ul, ms);:

/usr/bin/time ./c3c compile exprtk_functional_test_small.c3 
Program linked to executable 'exprtk_functional_test_small'.
1.62user 0.06system 0:01.05elapsed 159%CPU (0avgtext+0avgdata 107992maxresident)k
0inputs+1536outputs (0major+25033minor)pagefaults 0swaps

But when try to compile the full file it takes 98 seconds:

/usr/bin/time ./c3c compile exprtk_functional_test.c3 
Program linked to executable 'exprtk_functional_test'.
93.38user 0.33system 1:31.93elapsed 101%CPU (0avgtext+0avgdata 448864maxresident)k
0inputs+6168outputs (0major+137897minor)pagefaults 0swaps

See attached the files that I'm using to compile:

exprtk_functional_test.zip

@lerno
Copy link
Collaborator

lerno commented Sep 5, 2023

This doesn't make any sense at all. What Linux distro are you on? You could try a super simple program like:

fn void main()
{
	min(1, 2);
}

And see what happens.

@mingodad
Copy link
Author

mingodad commented Sep 5, 2023

Without the changes I showed above and using you simple example:

./c3c compile simple.c3 
 70: 		self.has_last = false;
 71: 		return IoError.EOF?;
 72: 	}
 73: 	usz n = min(readable, bytes.len);
     	        ^^^
(/home/mingo/dev/c/A_programming-languages/c3c/lib/std/io/stream/bytebuffer.c3:73:10) Error: Macros from other modules must be prefixed with the module name.

16: fn usz! ByteReader.read(&self, char[] bytes)
17: {
18: 	if (self.index >= self.bytes.len) return IoError.EOF?;
19: 	usz len = min(self.bytes.len - self.index, bytes.len);
    	          ^^^
(/home/mingo/dev/c/A_programming-languages/c3c/lib/std/io/stream/bytereader.c3:19:12) Error: The macro needs a path prefix (e.g. 'std::math::min').

 43: 		}
 44: 		self.refill()!;
 45: 	}
 46: 	usz n = min(self.write_idx - self.read_idx, bytes.len);
     	        ^^^
(/home/mingo/dev/c/A_programming-languages/c3c/lib/std/io/stream/buffer.c3:46:10) Error: The macro needs a path prefix (e.g. 'std::math::min').

20: fn usz! LimitReader.read(&self, char[] bytes)
21: {
22: 	if (self.limit == 0) return IoError.EOF?;
23: 	usz m = min(bytes.len, self.limit);
    	        ^^^
(/home/mingo/dev/c/A_programming-languages/c3c/lib/std/io/stream/limitreader.c3:23:10) Error: The macro needs a path prefix (e.g. 'std::math::min').

 55: 	while ((result = posix::pthread_mutex_trylock(mtx)) == errno::EBUSY)
 56: 	{
 57: 		if (!ms) break;
 58: 		ulong sleep = min(5, ms);
     		              ^^^
(/home/mingo/dev/c/A_programming-languages/c3c/lib/std/threads/os/thread_posix.c3:58:17) Error: The macro needs a path prefix (e.g. 'std::math::min').

@mingodad
Copy link
Author

mingodad commented Sep 5, 2023

My system is Ubuntu-18.04:

uname -a
Linux mingo-X550VX 5.4.0-137-generic #154~18.04.1-Ubuntu SMP Tue Jan 10 16:58:20 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

Notice that some time ago (months) I was able to build and execute it as I'm trying now.

@mingodad
Copy link
Author

mingodad commented Sep 5, 2023

When using -C it only take 100 milliseconds instead of 98 seconds:

/usr/bin/time ./c3c -C compile exprtk_functional_test.c3 
0.10user 0.02system 0:00.12elapsed 99%CPU (0avgtext+0avgdata 93864maxresident)k
0inputs+0outputs (0major+15736minor)pagefaults 0swaps

@mingodad
Copy link
Author

mingodad commented Sep 5, 2023

Here is a report where another related project also has trouble with compile/execute this files vovkos/jancy#8 (comment) .

@lerno
Copy link
Collaborator

lerno commented Sep 5, 2023

Ok, so the time it takes to compile this is because LLVM goes crazy trying to compile these things, but -O0 will give you somewhat better turnaround... or -O3.

-O0: 5s
-O3: 6s
"default": 86s

@mingodad
Copy link
Author

mingodad commented Sep 5, 2023

That's something that prabably can be fixed because clang/gcc have no trouble with then.
And about the errors with min (std::math::min) ?

@lerno
Copy link
Collaborator

lerno commented Sep 5, 2023

I finally found the bug @mingodad. It had to do with module ordering, so I had to hack the compiler to randomly order module evaluation until I got an ordering that reproduced the error. It's a special case that only occurs when two (or more) namespaced functions are registered before the builtin one. 😅

@mingodad
Copy link
Author

mingodad commented Sep 5, 2023

Yes it's building and compiling now, although slow for compile exprtk_functional_test.c3 but that is another problem.
Thank you !

@mingodad
Copy link
Author

mingodad commented Sep 5, 2023

And I still need to install the lib folder manually.

@lerno
Copy link
Collaborator

lerno commented Sep 5, 2023

I appreciate a patch for the CMake script to move the lib to an appropriate folder. It will be able to auto detect it if it's in the same dir as the binary or in a sub folder. I don't know the most Linux friendly placement.

@lerno
Copy link
Collaborator

lerno commented Sep 5, 2023

I'd like to add exprtk_functional_test.c3 as a test source, would you like to contribute it?

@mingodad
Copy link
Author

mingodad commented Sep 6, 2023

Yes no problem !
Notice that it is adapted from https://github.com/ArashPartow/exprtk that has MIT license:

[SECTION 03 - COPYRIGHT NOTICE]
Free  use  of  the  C++  Mathematical  Expression  Toolkit  Library is
permitted under the guidelines and in accordance with the most current
version of the MIT License.

https://www.opensource.org/licenses/MIT

@mingodad
Copy link
Author

mingodad commented Sep 6, 2023

Attached is the C from the C3 was adapted, noticed the difference in results, ideally they should be equal ?

gcc -o exprtk_functional_test-gcc exprtk_functional_test.c
./exprtk_functional_test-gcc
7364	7285
./exprtk_functional_test-c3
7364	5656

exprtk_functional_test_c.zip

@lerno
Copy link
Collaborator

lerno commented Sep 6, 2023

Well, if you say Clang, then probably, since it's going to share semantics with LLVM.

@mingodad
Copy link
Author

mingodad commented Sep 6, 2023

Here it compiled with clang:

clang-15-env /usr/bin/time clang -o exprtk_functional_test-clang exprtk_functional_test.c 
3.09user 0.09system 0:03.18elapsed 99%CPU (0avgtext+0avgdata 222372maxresident)k
0inputs+2112outputs (0major+50751minor)pagefaults 0swaps
./exprtk_functional_test-clang
7364	7285

/usr/bin/time gcc -o exprtk_functional_test-gcc exprtk_functional_test.c 
2.91user 0.20system 0:03.12elapsed 100%CPU (0avgtext+0avgdata 410020maxresident)k
0inputs+2040outputs (0major+123247minor)pagefaults 0swaps
./exprtk_functional_test-gcc
7364	7285

@lerno
Copy link
Collaborator

lerno commented Sep 6, 2023

I've noticed that if you don't enable SSE, then you'll get worse precision. Try --x86cpu=sse4 for example.

@mingodad
Copy link
Author

mingodad commented Sep 6, 2023

Good catch !
It only seems to happen with clang:

/usr/bin/time gcc -march=native -o exprtk_functional_test-gcc exprtk_functional_test.c 
2.69user 0.22system 0:02.92elapsed 99%CPU (0avgtext+0avgdata 398356maxresident)k
0inputs+1920outputs (0major+120087minor)pagefaults 0swaps
./exprtk_functional_test-gcc
7364	7285

clang-15-env /usr/bin/time clang -march=native -o exprtk_functional_test-clang exprtk_functional_test.c 
2.71user 0.10system 0:02.83elapsed 99%CPU (0avgtext+0avgdata 208804maxresident)k
456inputs+2040outputs (2major+47367minor)pagefaults 0swaps
./exprtk_functional_test-clang
7364	5655

/usr/bin/time gcc -O2 -march=native -o exprtk_functional_test-gcc exprtk_functional_test.c 
1.80user 0.09system 0:01.91elapsed 99%CPU (0avgtext+0avgdata 185460maxresident)k
136inputs+184outputs (1major+53034minor)pagefaults 0swaps
./exprtk_functional_test-gcc
7364	7285

clang-15-env /usr/bin/time clang -O2 -march=native -o exprtk_functional_test-clang exprtk_functional_test.c 
2.09user 0.07system 0:02.17elapsed 99%CPU (0avgtext+0avgdata 114820maxresident)k
0inputs+24outputs (0major+19857minor)pagefaults 0swaps
./exprtk_functional_test-clang
7364	5655

And it seems to start happen with clang-14, before that it doesn't degrade, probably a bug unnoticed so far ?

clang-10-env /usr/bin/time clang -O2 -march=native -o exprtk_functional_test-clang exprtk_functional_test.c 
0.97user 0.10system 0:01.29elapsed 83%CPU (0avgtext+0avgdata 114272maxresident)k
119424inputs+24outputs (486major+23397minor)pagefaults 0swaps
./exprtk_functional_test-clang
7364	7285

clang-11-env /usr/bin/time clang -O2 -march=native -o exprtk_functional_test-clang exprtk_functional_test.c 
0.96user 0.16system 0:01.35elapsed 83%CPU (0avgtext+0avgdata 120596maxresident)k
128048inputs+24outputs (532major+23758minor)pagefaults 0swaps
./exprtk_functional_test-clang
7364	7285

clang-12-env /usr/bin/time clang -O2 -march=native -o exprtk_functional_test-clang exprtk_functional_test.c 
4.10user 0.11system 0:04.44elapsed 94%CPU (0avgtext+0avgdata 117784maxresident)k
129112inputs+24outputs (515major+22383minor)pagefaults 0swaps
./exprtk_functional_test-clang
7364	7285

clang-13-env /usr/bin/time clang -O2 -march=native -o exprtk_functional_test-clang exprtk_functional_test.c 
4.16user 0.11system 0:04.52elapsed 94%CPU (0avgtext+0avgdata 124008maxresident)k
142944inputs+24outputs (582major+22784minor)pagefaults 0swaps
./exprtk_functional_test-clang
7364	7285

clang-14-env /usr/bin/time clang -O2 -march=native -o exprtk_functional_test-clang exprtk_functional_test.c 
2.13user 0.04system 0:02.18elapsed 99%CPU (0avgtext+0avgdata 109556maxresident)k
0inputs+24outputs (0major+19076minor)pagefaults 0swaps
./exprtk_functional_test-clang
7364	5655

@lerno
Copy link
Collaborator

lerno commented Sep 6, 2023

Oddly enough I get 7286 with C3 and --x86cpu=sse4. Do you see the same?

@mingodad
Copy link
Author

mingodad commented Sep 6, 2023

My compilers do not recognize --x86cpu=sse4:

clang-15-env /usr/bin/time clang -O2 --x86cpu=sse4 -o exprtk_functional_test-clang exprtk_functional_test.c 
clang-15: error: unsupported option '--x86cpu=sse4'
Command exited with non-zero status 1
0.00user 0.00system 0:00.01elapsed 100%CPU (0avgtext+0avgdata 41196maxresident)k
0inputs+0outputs (0major+1516minor)pagefaults 0swaps

/usr/bin/time gcc -O2 --x86cpu=sse4 -o exprtk_functional_test-gcc exprtk_functional_test.c 
gcc: error: unrecognized command line option ‘--x86cpu=sse4’
Command exited with non-zero status 1
0.01user 0.00system 0:00.01elapsed 94%CPU (0avgtext+0avgdata 3464maxresident)k
0inputs+0outputs (0major+204minor)pagefaults 0swaps

@mingodad
Copy link
Author

mingodad commented Sep 6, 2023

Ok it's a c3c option, on my machine it gives the same result as before:

./c3c compile -O2 --x86cpu=sse4 exprtk_functional_test.c3
./exprtk_functional_test
7364	5656

Looking through LLVM issues I found this llvm/llvm-project#54927 that links to this https://releases.llvm.org/14.0.0/tools/clang/docs/ReleaseNotes.html#floating-point-support-in-clang and I'm reading it to try understand the issue.

@mingodad
Copy link
Author

mingodad commented Sep 6, 2023

Odd enough it's because the change in -ffp-contract from off before clang-14 to on after it:

clang-15-env /usr/bin/time clang -O2 -ffp-contract=off -o exprtk_functional_test-clang exprtk_functional_test.c 
3.84user 0.04system 0:03.89elapsed 99%CPU (0avgtext+0avgdata 117452maxresident)k
128inputs+24outputs (1major+20505minor)pagefaults 0swaps

./exprtk_functional_test-clang
7364	7285

Probably c3c should expose that option too ?

@mingodad
Copy link
Author

mingodad commented Sep 6, 2023

Or internally stick with -ffp-contract=off ?

@lerno
Copy link
Collaborator

lerno commented Sep 6, 2023

This is an fma optimization that I do as well, following Clang. Turning it off is certainly possible.

@mingodad
Copy link
Author

mingodad commented Sep 6, 2023

Also I did the same some time ago #728 and I don't remember c3c taking too long to build exprtk_functional_test.c3 maybe bisecting can show what made it so slow ?

@lerno
Copy link
Collaborator

lerno commented Sep 6, 2023

There are two codegen options for "default" builds, and currently it takes slightly more optimized path. Usually that path is almost like the non-optimized one, but sometimes, like in this case, it completely blows up. I've discussed on the discord for C3 about changing the possible compilation defaults, as there are several things you might want:

  1. Fastest possible build, disregarding debug info and safety (-O0 -g0 -fast)
  2. Fast build running slow but safe (-O0 -g --safe)
  3. Safe build that isn't super fast to build (-O1 -g --safe)
  4. Safe build that is slow to build but offers the best perf (-O3 -g --safe)
  5. Same as 4 but prioritizing size (-Os -g --safe)
  6. Fast max perf unsafe no debug (-O3 -g0 --fast)
  7. Same as 6 but size (-Os -g0 --fast)
  8. Same as 6 but even smaller size (-Oz -g0 --fast)

Then finally 6-8 but with single module builds.

It THINK these are the main build options people will pick. There are certainly even more possibilities, but these are the main ones. So possibly people might need some broad categories. Clearly the default here is not great in all cases.

@lerno
Copy link
Collaborator

lerno commented Sep 6, 2023

Ok it's a c3c option, on my machine it gives the same result as before:

Even on -O0?

@mingodad
Copy link
Author

mingodad commented Sep 6, 2023

Which option is it ?

@lerno
Copy link
Collaborator

lerno commented Sep 6, 2023

./c3c compile -O0 --x86cpu=sse4 exprtk_functional_test.c3

@mingodad
Copy link
Author

mingodad commented Sep 6, 2023

Ok ! on my laptop I7 6º gen:

/usr/bin/time ./c3c compile -O0 --x86cpu=sse4 exprtk_functional_test.c3
Program linked to executable 'exprtk_functional_test'.
5.14user 0.39system 0:04.98elapsed 110%CPU (0avgtext+0avgdata 554248maxresident)k
0inputs+26104outputs (0major+152968minor)pagefaults 0swaps

./exprtk_functional_test
7364	7286

But there is machines that do not have sse4, what then ?

@lerno
Copy link
Collaborator

lerno commented Sep 6, 2023

Actually it's the other way around. sse4 disables the fma instruction in avx so that fma instructions aren't generated.

@mingodad
Copy link
Author

mingodad commented Sep 6, 2023

This mean that without --x86cpu=sse4 binaries produced by c3c will not work on machines without sse4 ?

@lerno
Copy link
Collaborator

lerno commented Sep 6, 2023

Yes, you can use --x86cpu=baseline to target any x64 processor. Possible CPU targets in order are:
baseline, ssse3, sse4, avx1, avx2-v1, avx2-v2 (Skylake/Zen1+), avx512 (Icelake/Zen4+). Aside from native.

@mingodad
Copy link
Author

mingodad commented Sep 6, 2023

Wouldn't better if --x86cpu=baseline be the default instead ?

@data-man
Copy link
Contributor

data-man commented Sep 6, 2023

With $$set_rounding_mode(0); (fixed in #978) I got:

7364 1764

@lerno
Copy link
Collaborator

lerno commented Sep 6, 2023

No, because baseline would be the very first AMD 64 bit processors, which are very very old at this point, and pretty much no-one has them. So optimizing for that would not be reasonable.

@mingodad
Copy link
Author

mingodad commented Sep 7, 2023

With the latest changes the build time is fine now :

/usr/bin/time ./c3c compile  exprtk_functional_test.c3
Program linked to executable 'exprtk_functional_test'.
4.85user 0.31system 0:04.65elapsed 110%CPU (0avgtext+0avgdata 534896maxresident)k
0inputs+23560outputs (0major+142742minor)pagefaults 0swaps

The output:

/usr/bin/time ./exprtk_functional_test
7364	5656
0.01user 0.00system 0:00.02elapsed 95%CPU (0avgtext+0avgdata 5428maxresident)k
0inputs+0outputs (0major+229minor)pagefaults 0swaps

Adding --x86cpu=sse4:

/usr/bin/time ./c3c compile --x86cpu=sse4 exprtk_functional_test.c3
Program linked to executable 'exprtk_functional_test'.
4.86user 0.33system 0:04.66elapsed 111%CPU (0avgtext+0avgdata 546336maxresident)k
0inputs+23640outputs (0major+148332minor)pagefaults 0swaps

Output:

/usr/bin/time ./exprtk_functional_test
7364	7286
0.00user 0.00system 0:00.00elapsed 100%CPU (0avgtext+0avgdata 5368maxresident)k
0inputs+0outputs (0major+228minor)pagefaults 0swaps

Would be nice to get the last output without any extra command line to the c3c compiler.

@lerno
Copy link
Collaborator

lerno commented Sep 12, 2023

It should default to not doing the optimization now.

@mingodad
Copy link
Author

Thank you !
I think that this can be closed now !

@lerno
Copy link
Collaborator

lerno commented Sep 12, 2023

Thank you for testing things!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants